Hi
I tried to extract 19 characters (default) enclosed with in tag from a file using cut command. If the characters comprises of double space, the cut command gives the output with a single spacing.
file 1
<name>Kumar Rajasekaran</name>
cut -c7-26 "file1"
the out put i received is
Kumar Rajasekaran
Expected output
Kumar Rajasekaran
Please guide me.
Thanks
clx
May 27, 2010, 3:33am
2
Will this work for you?
$ echo '<name>Kumar Rajasekaran</name>' | awk -F "<name>|</name>" '{print $2}'
Kumar Rajasekaran
$
This will preserve the spaces.
Well, I tried with cut and it reserves the space too.:rolleyes:
And try with sed too..
echo "<name>Kumar Rajasekaran</name>" | sed 's/<name>\(.*\)<\/name>/\1/'
Thanks,
Please have a look on my code. Please let me know how to use awk command as mentioned above.
source file input
name>Rajesh vadakar IIT Chennai </name>
Expected output
Rajesh vadakar IIT Chennai
c_line="/apps/source.txt"
targetfile="result.txt"
var1=0
while read c_line
do
var1=`grep -c "IIT Chennai" $c_line`
echo $var1 >> $targetfile
if [ $var1 -ge 1 ]
then
val=`cut -c6-37 $c_line`
echo $var >> $targetfile
fi
done<${c_line}
Please update me as earliest.
Thanks
---------- Post updated at 02:28 PM ---------- Previous update was at 02:27 PM ----------
my above code is also not working. Please correct my mistake.
replace
val=`cut -c6-37 $c_line`
with
val=`echo $$c_line | cut -c6-37 `
cheers,
Devaraj Takhellambam
Hi,
Still its not working. It is considering newspace as blank line. so the above name tag value is spiltted in to differents columns.
pleasre review my code and correct me
Had an extra $ in my code above
val=`echo $c_line | cut -c6-37 `
HI
i included as u said. But while reading itself, it is suppressing blank spaces more than one occurance to one. so i am getting wrong data. Please review my code
c_line="/apps/source.txt"
targetfile="result.txt"
var1=0
while read c_line
do
var1=`grep -c "IIT Chennai" $c_line`
echo $var1 >> $targetfile
if [ $var1 -ge 1 ]
then
val=`cut -c6-37 $$c_line`
echo $var >> $targetfile
fi
done<${c_line}
clx
May 27, 2010, 6:51am
9
Have you getting any error?
can you post the content of "/apps/source.txt" ?
Sekar1
May 27, 2010, 7:11am
10
As u said, i included, but in vain. Please correct me if i am doing wrong.
while read c_line
do
echo $c_line >> $targetfile
var1=`grep -c "IIT Chennai" $c_line`
if [ $var1 -ge 1 ]
then
val=`echo $$c_line | cut -c6-37 `
echo $var >> $targetfile
fi
done<${c_line}]
Ok, plz tell me what is the I/P contents and what is the expected O/P. We will provide you the solution depending on that.
Sekar1
May 27, 2010, 7:36am
13
i am not getting anything the final.txt file. please find the execution result.
$. ./file.sh
grep: can't open <Record><s
grep: can't open o>1</s
grep: can't open o><empid>E0001</empid><
grep: can't open ame>Rejsh
grep: can't open suderam</
grep: can't open ame><college>IIT
grep: can't open Che
grep: can't open
grep: can't open ai
grep: can't open </college>
[: -ge: unary operator expected
$ cat final.txt
---------- Post updated at 05:06 PM ---------- Previous update was at 05:06 PM ----------
My input file has the following data
<Record><sno>1</sno><empid>E0001</empid><name>Rejsh suderam</name><college>IIT Chennai </college>
clx
May 27, 2010, 7:40am
14
while read c_line
You are modifying c_line variable. and now this contains the each line of the input file.
and grep expects a filename
use another variable name in while.
And use like this:
echo $line_var | grep ....
Sekar1
May 27, 2010, 7:41am
15
I have space in the input tag college. The tag college holds the value as "IIT Chennai " and i want the output exaclty with the spaces given in input between IIT and chennai
Expected OUTPUT
---------------
"IIT Chennai"
clx
May 27, 2010, 7:47am
16
Again, can't you use awk ??
$ awk -F "<name>|</name>" '{print $2}' x
Rejsh suderam
Rejsh suderam
Rejsh suderam
Rejsh suderam
$ 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>
$
---------- Post updated at 05:17 PM ---------- Previous update was at 05:14 PM ----------
If "IIT Chennai":
$ awk -F "<college>|</college>" '{print $2}' x
IIT Chennai
IIT Chennai
IIT Chennai
IIT Chennai
$
Sekar1
May 27, 2010, 7:53am
17
I did as you mentioned. Please review.
while read c_line
do
echo $c_line >> $targetfile
var1=`echo $c_line | grep -c "IIT Chennai"`
#echo $var1 >> $targetfile
if [ $var1 -ge 1 ]
then
#val=`cut -c7-75 $$c_line`
#val=`echo $$c_line | cut -c78-94 `
val=`awk -F "<college>|</college>" '{print $2}'`
echo $var >> $targetfile
fi
done<${c_line}
I am not getting any output while executing the script file.
#cat source.txt
<Record><sno>1</sno><empid>E0001</empid><name>Rejsh suderam</name><college>IIT Chennai </college>
output
--------
$ cat 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;
fi;
done< file
cheers,
Devaraj Takhellambam
clx
May 27, 2010, 8:01am
19
sekar1:
I did as you mentioned. Please review.
I am not getting any output while executing the script file.
#cat source.txt
<Record><sno>1</sno><empid>E0001</empid><name>Rejsh suderam</name><college>IIT Chennai </college>
output
--------
$ cat final.txt
$
You don't need to read the file if you are using awk.
if you want the output as
<input line 1>
IIT Chennai
<input line 2>
IIT Chennai
try just:
awk -F "<college>|</college>" '{print $0"\n"$2}' source.txt > final.txt
You can as well do just but i wrote the script using sed above just as yu wanted.
sed 's/.*<college>\(.*\) <\/college>$/\1/' file