tags:

views:

91

answers:

5

This is the code

for f in tmp_20100923*.xml  
do  
 str1=`more "$f"|grep count=`  
 i=`echo $str1 | awk -F "." '{print($2)}'`  
 j=`echo $i | awk -F " " '{print($2)}'` // output is `count="0"`  
 sed 's/count=//g' $j > $k; echo $k;   
done

I tried to get value 0 from above output using sed filter but no success. Could you please advise how can i separate 0 from string count="0" ?

A: 

you are trying to sed a file whose name is $j

Instead you can

echo $j | sed 's/count=//g'
djna
this is also true, as the third argument to sed is the file. from the man page `sed [OPTION]... {script-only-if-no-other-script} [input-file]...`
Dan Beam
A: 

This should snag everything between the quotes.

sed -re 's/count="([^"]+)"/\1/g'

-r adds --regexp-extended to be able to cool stuff with regular expressions, and the expression I've given you means:

search for count=",
then store ( any character that's not a " ), then
make sure it's followed by a ", then
replace everything with the stuff in the parenthesis (\1 is the first register)
Dan Beam
+1  A: 

Remove all non digits from $j:

echo ${j//[^0-9]/}
eumiro
A: 

You can use this sed regexp:

sed 's/count="\(.*\)"/\1/'

However your script has another problem:

j=`echo $i | awk -F " " '{print($2)}'` // output is `count="0"`  
sed 's/count=//g' $j > $k; echo $k;   

should be

j=`echo $i | awk -F " " '{print($2)}'` // output is `count="0"`  
echo $j | sed 's/count=//g'   

or better:

echo $i | awk -F " " '{print($2)}' | sed 's/count=//g'

'sed' accepts filenames as input. $j is a shell variable where you put the output of another program (awk).

Also, the ">" redirection puts things in a file. You wrote ">$k" and then "echo $k", as if >$k wrote the output of sed in the $k variable.

If you want to keep the output of sed in a $k variable write instead:

j=`echo $i | awk -F " " '{print($2)}'` // output is `count="0"`  
k=`echo $j | sed 's/count=//g'`
ithkuil
+1  A: 

You can have AWK do everything:

for f in tmp_20100923*.xml  
do  
    k=$(awk -F '.' '/count=/ {split($2,a," "); print gensub("count=","","",a[2])}')
done

Edit:

Based on your comment, you don't need to split on the decimal. You can also have AWK do the summation. So you don't need a shell loop.

awk '/count=/ { sub("count=","",$2); gsub("\042","",$2); sum += $2} END{print sum}' tmp_20100923*.xml
Dennis Williamson
yah, any time you have a long sequence of grep, sed and awk, it's almost always better to just replace it with a short awk program.
Zac Thompson
Thanks, it is really helpful :-)
Ashish