Basically, "awk -F" replaces if-else while for statements right?! And works with data in file (Because -F) Am I right?
Not exactly. The while loop is side effect of the fact that awk reads a line from the input file and performs the actions associated with each set of conditions that match those conditions. So in the awk program:
Notes:
The 1st line of this shell script says that we are going to use the awk utility to process an awk script, sets the field separator to the period character (-F '[.]', and the trailing single quote starts the awk script that specifies how awk will process the files it reads.
The 2nd line in the script checks to see if the 3rd field in the current line is greater than the current value of m3 (which has the initial value 0) and, if it is, sets m3 (the maximum 3rd field value seen) to the value of the 3rd field on this line, sets m4 (the maximum 4th field value seen so far associated with the current 3rd field maximum value) to the value of the 4h field on this line, and skips to the next line without checking any of the other conditions.
The 3rd line in the script checks to see if m3 is equal to the 3rd field in the current line and the 4th field in the current line is greater than the current value of m4 and, if it is, sets m4 to the value of the 4th field on this line.
The END condition on the 4th line is special. It is only true after the end-of-file condition has been reached on all of the input files. The 1st statement in the END section actions checks to see if the largest value found in the 4th field in any line with the largest value in the 3rd field in any line in the file is 254 and, if so, increment the 3rd field max value and set the 4th field max value to 0. Then (whether m3 and m4 were changed or not) prints the output line with the computed m3 and m4 values.
The last line of the script has a closing brace that terminates the END clause, a single quote that terminates the awk script, and a shell pathname matching pattern that will be expanded by the shell to be a list of files to be processed by the awk script.
The if statement in the END clause could be changed to a separate awk statement with the condition and actions:
but it is more efficient to only test for that condition once in the END clause instead of checking for it on every input line when m4 is set to a new value.
This User Gave Thanks to Don Cragun For This Post:
My HP-UX 11.0 system is supporting an Oracle database. I have found a number of references on the Net to the "Max I/O size" in relation to setting Oracle parameters.
How can I tell what my max i/o size is? I originally made the assumption that it was referring to my stripe size but now I think... (1 Reply)
My code below is supposed to find which company had the most business and then print the appropriate fields from another file which are the companies ID number and name. I can loop through awk and display all the total amount of business for each company but I need help in only printing out the... (1 Reply)
I have a file like:
<word> 5
<word> 3
<word> 5
<word> 2
<word> 6
<word> 8
<word> 12
and i need to know the max value of the second column, in this case 12.
Plz help me!
Actually i need the TOTAL, AVERANGE and MAX VALUE and i'm using this in... (10 Replies)
Hi All,
I want to choose the maximum value from a column values.
eg -
234
23
567
43
2456
5
678
8978
from thses values how to choose the max value ?
thanks.
roger. (3 Replies)
1> Can anyone tell me what is the maximum no of parameters that can be passed to a file
2> i have 100 files amongst which in 500 files i have a string which needs to be searched how would get the names of those files. (1 Reply)
From reading my C book, Im aware that the integers have a maximum value which depends on what type of processor you are using (since they use 16-bit or 32-bit instructions).
Now I know pointers are very flexible, since they can reference anything, but in the case of integer pointers, can they... (4 Replies)
Hi All,
I have this userid:
# cat /etc/shadow | grep mysql
mysql:$1$O12345678901234/.:123456:0::14:::
Max password expiration is blank. What does it mean?
If it's like this below
mysql:$1$O12345678901234/.:123456:0:99999:14:::
then there's no expiration.
Thanks in advance for... (1 Reply)
without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
I've the following data set. I would like to look at the column 3 and only use the rows which has the max value for column 3
Can we use the awk or sed to achieve it.
10 2 10 100
11 2 20 100
12 2 30 100
13 2 30 100
14 ... (7 Replies)