Help understanding "awk" code
Hello, I have a script, containing following awk code:
Code:
Code:
458 0 0 Note: ${IFILE} file mentioned in code, contains values like: Code:
value=0 |
Code:
/value=0\.[^0]/{cat2++;} [^0] -> all but a zero Code:
/value=0\.0/{cat3++;} BTW: Please stop using large font, there's really no need for that. |
I presume the confusion is from teh lack of formal structures around the confiditional operations there.
/value=0\.0/{cat3++;} means something like Code:
if {string matches regex "value=0\.0" ) |
Code:
grep " RESULT " ${IFILE} \ |
I think my explanation covers that just fine.
|
Quote:
BTW, problem is that I have got an old script created by someone who left the job, and I have been given a task to write a new script which should have same functionality like this script doing, and that is why I want to fully understands its code. |
Would this help;
Code:
awk 'BEGIN{sum=0; cat1=0; cat2=0; cat3=0;} The green part is done for every line of input (lines that contain RESULT (with extra spaces)). sum++ line -> counts all the lines it gets (total number of lines) cat2++ line -> counts all the lines that contain value=0.<whatver> as long as it is _not_ value=0.0 cat3++ line ->counts all the lines that contain value=0.0 the brown part, some calculations and then printing the 3 entries, is done when all the lines are parsed. Quote:
- What you posted will not work (grep " RESULT " ${IFILE} \ should be grep " RESULT " ${IFILE} | \ - No need for the grep part, this can be done by awk - the sum counter isn't needed in this case (the awk NR variable can be used) That's without knowing what exactly needs to be done with the lines it gets. Some resources that might help: Bash resources:
Sed/Awk resources: General resources: |
Special thanks to @druuna. You've explained it very well. One more last question:-
Can we devide this code in two parts (for understanding purpose) as follow: Part-1: Code:
grep " RESULT " ${IFILE} \ Code:
END{cat1=sum-(cat2+cat3); print cat1, cat2, cat3;}' (1) In first part, it's searching for specified patterns & then counting values. And after finishing, it simply prints those values in second part of the code? (2) Is it looping between BEGIN and END of the code? Or calculating all specified patterns in one shot, e.g. in 4th line it searches for all values that are equal to 0.0<whatever>, and go to next line? (3) After calculating values e.g. in 4th line, does it store that total connt in cat2? Or is it like a loop in which it search for a pattern, get pattern, and add 1 count? |
the BEGIN{} block runs before any of the input data is processed, the END{} block runs afterwards, everything else in the main {} block is executed on a per input line basis. so after each input line is read, the 4th line does get executed completely, but of course the value of cat2 persists over these executions, increasing each time it is matched.
|
Code:
BEGIN{sum=0; cat1=0; cat2=0; cat3=0;} |
Many thanks everyone, my purpose of asking this question has solved.
Special thanks to @druuna for always being helpful :) |
Quote:
I had a case just the other day where I needed it to print an actual "0" if the variable never incremented, which it won't do unless explicitly set first. I think the OP code here may come up against the same issue. Speaking of which, the above section can be shortened a bit: Code:
BEGIN{ sum=cat1=cat2=cat3=0 } |
All times are GMT -5. The time now is 10:57 PM. |