Include white spaces while using CUT command

Hi Thanks for your response. But eventhough i didn't get the expected output
I am pasting you for your reference.

bash: file: No such file or directory

---------- Post updated at 06:01 PM ---------- Previous update was at 06:00 PM ----------

i got the above output. I corrected my code as per your suggestion.

file=/apps/source.txt
targetfile="final.txt"
while read line;
do if [ `echo $line | grep -c "IIT Chennai"` -gt 0 ]; then
var=`echo $line | sed 's/.*<college>\(.*\) <\/college>$/\1/'`;
echo $var >> $targetfile;
fi;
done< file

replace

done< file

with

done< yourfilename

Hi, i updated like $file in end, but till in vain.
file=/apps/source.txt
targetfile="final.txt"
while read line;
do if [ `echo $line | grep -c "IIT Chennai"` -gt 0 ]; then
var=`echo $line | sed 's/.*<college>\(.*\) <\/college>$/\1/'`;
echo $var >> $targetfile;
fi;
done< ${file}

---------- Post updated at 06:09 PM ---------- Previous update was at 06:08 PM ----------

sorry for appending the code wrongly.

file=/apps/source.txt
targetfile="final.txt"
while read line;
do if [ `echo $line | grep -c "IIT Chennai"` -gt 0 ]; then
var=`echo $line | sed 's/.*<college>\(.*\) <\/college>$/\1/'`;
echo $var >> $targetfile;
fi;
done< ${file}

what error are you getting now?

Hi,

I am not getting any error, at the same time no output in the final.txt.

Ok. show me the o/p of the below

file=/apps/source.txt
targetfile="final.txt"
while read line;
do 
if [ `echo $line | grep -c "IIT Chennai"` -gt 0 ]; then
var=`echo $line | sed 's/.*<college>\(.*\) <\/college>$/\1/'`;
echo $var >> $targetfile;
else
echo "No match found"
fi;
done< ${file}

I got output as "No Match found"

Ok good. try this

file=/apps/source.txt
targetfile="final.txt"
while read line;
do 
if [ `echo $line | grep -c "IIT *Chennai"` -gt 0 ]; then
var=`echo $line | sed 's/.*<college>\(.*\) <\/college>$/\1/'`;
echo $var >> $targetfile;
else
echo "No match found"
fi;

done< ${file}

Again the same output "No Match found"

And what's the output of the following command ?

cat /apps/source.txt

Copy/paste the command and its output from your Terminal screen.

tyler_durden

Hi,

Please proivide me updates. I am really facing pbm in printing the word with spaces as it was given in source file.
Thanks in advance.

---------- Post updated at 06:55 PM ---------- Previous update was at 06:52 PM ----------

source file holds the below input

"<Record><sno>1</sno><empid>E0001</empid><name>Rejsh suderam</name><college>IIT Chennai </college> "

I have multiple spaces between IIT and chennai inside college tag. Please help me out to resolve this problem.

In that case you should have no problem in showing the output from your Terminal screen.

Run the following command:

cat /apps/source.txt

and copy/paste the relevant portion from your Terminal screen.
Use code tags as well.

tyler_durden

Dear Sekar1,

There are at least 3 solutions we already provided. Either you are having some typos (if tried) or you didn't try them.

Mentioning again from my previous post:

$ awk -F "<college>|</college>" '{print $2}' x
IIT  Chennai 
IIT Chennai 
IIT     Chennai 
IIT   Chennai 
$ cat x
<Record><sno>1</sno><empid>E0001</empid><name>Rejsh suderam</name><college>IIT  Chennai </college>
<Record><sno>1</sno><empid>E0001</empid><name>Rejsh suderam</name><college>IIT Chennai </college>
<Record><sno>1</sno><empid>E0001</empid><name>Rejsh suderam</name><college>IIT     Chennai </college>
<Record><sno>1</sno><empid>E0001</empid><name>Rejsh suderam</name><college>IIT   Chennai </college>
$ 

See the spaces between IIT and Chennai.
If you are having another requirement, please do mention that clearly.

Pasted as per your request. Please guide me

Deleted. Duplicate.

I have multiple spaces between IIT and chennai, but its not displaying in the screen as above

---------- Post updated at 07:07 PM ---------- Previous update was at 07:06 PM ----------

Sorry, in the terminal i can able to see the multiple spaces, but while pasting the spaces are trimmed between IIT and chennai.

---------- Post updated at 07:11 PM ---------- Previous update was at 07:07 PM ----------

its not a scenario that i shouldn't use awk. But i need it in the script because, i need to extract all "IIT chennai" as it is occuring in source file and i need to print both college tag value and the name value. please pass me if awk suits to above requirement, i will replace it in the code.
Thanks

---------- Post updated at 07:12 PM ---------- Previous update was at 07:11 PM ----------

while read line;
do
if [ `echo $line | grep -c "IIT *Chennai"` -gt 0 ]; then
var=`echo $line | sed 's/.*<college>\(.*\) <\/college>$/\1/'`
echo $var >> $targetfile
else
echo "No match found"
fi;
done< ${file}

In the above code, pls update me where do i need to use awk command.

That's okay.
If your shell script is "my_script.sh", then run the following commands and copy/paste the output from your Terminal screen.

rm final.txt
cat my_script.sh
. my_script.sh
cat final.txt

If your shell script is named something other than "my_script.sh", then put that name in the 2nd and 3rd command above.

tyler_durden

Pls find the details.

$ cat source.txt
<Record><sno>1</sno><empid>E0001</empid><name>Rejsh   suderam</name><college>IIT                    Chennai   </college>
$ rm final.txt
$. ./css2.sh
$ cat final.txt
IIT Chennai

Pls do the following and post the output within code tags:

cat -vet source.txt

$ cat -vet source.txt
<Record><sno>1</sno><empid>E0001</empid><name>Rejsh suderam</name><college>IIT Chennai </college> $

---------- Post updated at 07:29 PM ---------- Previous update was at 07:29 PM ----------

sorry, pls find the output

 $ cat -vet source.txt
<Record><sno>1</sno><empid>E0001</empid><name>Rejsh   suderam</name><college>IIT                    Chennai   </college> $