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}
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;
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.
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.