ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.