searching for 2 strings with actions
I am searching for 2 strings, both on different lines, and then performing a simple action. The awk that I am using comes with Solaris 8 [Ancient, I know].
I looked in the awk 1 liners textfile, and could'nt find any examples for performing multiple actions when searching for 2 or more strings. I think that the problem is with the syntax, but after some experimentation and Google searches, I can't see what is wrong. Code:
awk -F" " '/Avg / {print $8} && /Max / {print $8}' filename |
Quote:
Must the action be taken when both strings are found: Both Avg and Max must be present and only then do something. -or- Action must be taken when either of them are present: Avg is seen -> do something and/or Max is seen -> do something. You might want to post a relevant example of the input and the expected output. Quote:
Quote:
I do see one thing that seems to be unnecessary: A space is the default filed separator in awk, so the -F" " part isn't needed. |
Hi, thanks for the feedback.
There will always be Avg and Max in the textfile but I would go for "Action must be taken when either of them are present: Avg is seen -> do something and/or Max is seen -> do something." I just realised that I need to use a printf, so that a CR or LF are not sent in the output. Code:
-------Reads/s---------Writes/s---------IOPS-----------KBRead/s--------------KBWrite/s-------------asvc_t--%Wait--%DiskBusy- Code:
458,702 |
Quote:
Code:
awk '/Avg/ { avg = $8 } /Max/ { max = $6 } END{ printf"%s,%s\n", avg, max }' infile |
What might not of been clear from my side, was that I have many thousands of these entries in a textfile.
Does'nt the END statement, mean to only loop once, so in effect it will only print the output from the last matching line of Avg and Max whereas I would like to print out each match in CSV format. |
Quote:
Quote:
If I make the assumption that every Avg will be followed by a Max then this should work: Code:
awk '/Avg/ { printf $8 } /Max/ { print "," $6 } ' infile Code:
$ cat infile |
Ah then I made this more complicated than it really way. Sorry again for the confusion and many thanks for showing me the right approach, that worked as I needed it to.
|
All times are GMT -5. The time now is 09:33 PM. |