LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Awk query (http://www.linuxquestions.org/questions/linux-newbie-8/awk-query-4175439547/)

shivaa 12-01-2012 05:03 AM

Awk query
 
Hi everyone,
I have a script with following lines:
Code:

grep " string " filename \
awk 'BEGIN{sum=0; cat1=0; cat2=0; cat3=0;}
{sum++}
/testval=0\.[^0]/{cat2++;}       
/testval=0\.0/{cat3++;}
END{cat1=sum-(cat2+cat3); print cat1, cat2, cat3;}'

Everything's fine, but I don't understand that what pattern it's searching in line marked in red? Is it summing up all testval value that are greater than 0.0 (i.e. 0.1, 0.2... and so on)? Or does it mean that take all values that begin with a 0 after decimal point?
Also what's difference between lines marked in red line marked in blue?

druuna 12-01-2012 05:09 AM

Wasn't this already discussed in your other thread?

Help understanding "awk" code

Especially this post (and the follow ups): post #7

shivaa 12-01-2012 05:20 AM

@druna: Yes, but a little query! in your answer you had mentioned following:
Quote:

Code:

/value=0\.[^0]/{cat2++;} increment cat2 if not value=0.0
[^0] -> all but a zero
I couldn't understand what your comment "all but a zero" saying? A carrot sign (^) means all values begginning with the character after it.. Am I right? Then it should consider all values which are 0.0<whatever>
Please correct me if I explained it wrong.

druuna 12-01-2012 05:27 AM

Quote:

Originally Posted by shivaa (Post 4840660)
I couldn't understand what your comment "all but a zero" saying?

all but a zero In different wording -> everything except zero

Quote:

A carrot sign (^) means all values begginning with the character after it.. Am I right? Then it should consider all values which are 0.0<whatever>
That isn't correct. A carrot inside a range means not

Code:

^[0-9] -> A number at the beginning of the line,
[^0-9] -> Not a number,
^[^0-9] -> Not a number at the beginning of the line.

Have a look here: Regex Tutorial, Examples and Reference / Regular Expression Basic Syntax Reference

shivaa 12-01-2012 05:53 AM

Great! Thanks for making it clear. ;)


All times are GMT -5. The time now is 09:07 AM.