LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-26-2013, 02:05 AM   #1
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Rep: Reputation: 17
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

Last edited by dazdaz; 07-26-2013 at 02:57 AM.
 
Old 07-26-2013, 03:36 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by dazdaz View Post
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.
 
Old 07-26-2013, 04:59 AM   #3
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Original Poster
Rep: Reputation: 17
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

Last edited by dazdaz; 07-26-2013 at 05:00 AM.
 
Old 07-26-2013, 05:11 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by dazdaz View Post
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

Last edited by druuna; 07-26-2013 at 05:17 AM. Reason: Changed printf / print to one printf
 
1 members found this post helpful.
Old 07-26-2013, 06:35 AM   #5
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Original Poster
Rep: Reputation: 17
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.
 
Old 07-26-2013, 07:02 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by dazdaz View Post
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....
 
1 members found this post helpful.
Old 07-26-2013, 07:12 AM   #7
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Original Poster
Rep: Reputation: 17
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching and replacing strings in groups of files jebradl Linux - Newbie 10 06-22-2012 03:20 PM
grep searching for strings with '(apostrophe) macsdev Programming 5 11-11-2010 11:46 PM
[SOLVED] Searching and replacing strings in a file with strings in other files xndd Linux - Newbie 16 07-29-2010 02:40 PM
Searching through strings in StarBasic/OpenOffice BASIC scuffell Programming 1 03-31-2006 09:50 AM
Searching files for strings tmoorman Linux - Software 4 01-08-2004 01:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration