LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   searching for 2 strings with actions (https://www.linuxquestions.org/questions/programming-9/searching-for-2-strings-with-actions-4175470965/)

dazdaz 07-26-2013 02:05 AM

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
awk: syntax error near line 1
awk: bailing out near line 1


druuna 07-26-2013 03:36 AM

Quote:

Originally Posted by dazdaz (Post 4997035)
I am searching for 2 strings, both on different lines, and then performing a simple action.

I'm not sure I understand what you want to do.

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:

The awk that I am using comes with Solaris 8 [Ancient, I know].
Solaris comes with more then one awk version. The default one is not as flexible as the one that resides in /usr/xpg4/bin/awk (which I would use).

Quote:

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
awk: syntax error near line 1
awk: bailing out near line 1


Without knowing exactly what it is you want to do, I can only speculate (which I won't).

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.

dazdaz 07-26-2013 04:59 AM

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-
                        Avg  429.2 ( 94%)    28.6 (  6%)    458 (  0.46K)    4078 (  3.98MB/s)    172 (  0.2MB/s)    15.3ms    0.0%    57%
                        ----------------------------------------------------------------------------------------------------------------------------
                        Max  644.6( 0.64K)  279.5( 0.28K)  702 (  0.70K)    8655 (  8.45MB/s)    1649 (  1.6MB/s)    113.5ms    1.5%    93%
                        ----------------------------------------------------------------------------------------------------------------------------

Expected output :
Code:

458,702

druuna 07-26-2013 05:11 AM

Quote:

Originally Posted by dazdaz (Post 4997110)
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-
                        Avg  429.2 ( 94%)    28.6 (  6%)    458 (  0.46K)    4078 (  3.98MB/s)    172 (  0.2MB/s)    15.3ms    0.0%    57%
                        ----------------------------------------------------------------------------------------------------------------------------
                        Max  644.6( 0.64K)  279.5( 0.28K)  702 (  0.70K)    8655 (  8.45MB/s)    1649 (  1.6MB/s)    113.5ms    1.5%    93%
                        ----------------------------------------------------------------------------------------------------------------------------

Expected output :
Code:

458,702

Looking at the provided input I notice that the Avg line looks different (layout) the the Max line. Assuming the input shown is correct:
Code:

awk '/Avg/ { avg = $8 } /Max/ { max = $6 } END{ printf"%s,%s\n", avg, max }' infile
458,702


dazdaz 07-26-2013 06:35 AM

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.

druuna 07-26-2013 07:02 AM

Quote:

Originally Posted by dazdaz (Post 4997138)
What might not of been clear from my side, was that I have many thousands of these entries in a textfile.

That was indeed not clear from your previous post. It will also make my previous solution useless.

Quote:

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.
The END part is only used once all the lines in the infile are processed.

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
Example run:
Code:

$ cat infile
                        -------Reads/s---------Writes/s---------IOPS-----------KBRead/s--------------KBWrite/s-------------asvc_t--%Wait--%DiskBusy-
                        Avg  429.2 ( 94%)    28.6 (  6%)    458 (  0.46K)    4078 (  3.98MB/s)    172 (  0.2MB/s)    15.3ms    0.0%    57%
                        ----------------------------------------------------------------------------------------------------------------------------
                        Max  644.6( 0.64K)  279.5( 0.28K)  702 (  0.70K)    8655 (  8.45MB/s)    1649 (  1.6MB/s)    113.5ms    1.5%    93%
                        ----------------------------------------------------------------------------------------------------------------------------
                        Avg  429.2 ( 94%)    28.6 (  6%)    458 (  0.46K)    4078 (  3.98MB/s)    172 (  0.2MB/s)    15.3ms    0.0%    57%
                        ----------------------------------------------------------------------------------------------------------------------------
                        Max  644.6( 0.64K)  279.5( 0.28K)  702 (  0.70K)    8655 (  8.45MB/s)    1649 (  1.6MB/s)    113.5ms    1.5%    93%
                        ----------------------------------------------------------------------------------------------------------------------------
                        Avg  429.2 ( 94%)    28.6 (  6%)    458 (  0.46K)    4078 (  3.98MB/s)    172 (  0.2MB/s)    15.3ms    0.0%    57%
                        ----------------------------------------------------------------------------------------------------------------------------
                        Max  644.6( 0.64K)  279.5( 0.28K)  702 (  0.70K)    8655 (  8.45MB/s)    1649 (  1.6MB/s)    113.5ms    1.5%    93%
                        ----------------------------------------------------------------------------------------------------------------------------
$ awk '/Avg/ { printf $8 } /Max/ { print "," $6 } ' infile
458,702
458,702
458,702

If this still doesn't do what you want then provide an infile example that is actually relevant....

dazdaz 07-26-2013 07:12 AM

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.