Translate grep to awk | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Translate grep to awk

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-06-2013
SkySmart SkySmart is offline
Registered User
 
Join Date: Dec 2006
Last Activity: 11 December 2014, 6:13 PM EST
Posts: 655
Thanks: 418
Thanked 2 Times in 2 Posts
Translate grep to awk


Code:
sed -n "2,10p" lfile | egrep error | egrep -vc memory

sed -n "2,10p" lfile | egrep error | egrep -v memory

sed -n "2,10p" lfile | egrep error | egrep -c memory

sed -n "2,10p" lfile | egrep error | egrep memory

above are four separate commands. i want to combine the grep in each command into one awk statement. instead of using two egreps, i figure awk can do it better.

desired code would be:


Code:
sed -n "2,10p" lfile | awk....

sed -n "2,10p" lfile | awk....

sed -n "2,10p" lfile | awk....

sed -n "2,10p" lfile | awk.....

os: sunos, linux redhat, ubuntu, hpux, aix

Last edited by SkySmart; 01-06-2013 at 08:52 AM..
Sponsored Links
    #2  
Old 01-06-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 19 December 2014, 6:33 PM EST
Location: Galactic Empire
Posts: 3,395
Thanks: 240
Thanked 1,211 Times in 1,137 Posts

Code:
sed -n "2,10p" lfile | awk '/error/&&!/memory/{c++}END{print c}'
sed -n "2,10p" lfile | awk '/error/&&!/memory/'
sed -n "2,10p" lfile | awk '/error/&&/memory/{c++}END{print c}'
sed -n "2,10p" lfile | awk '/error/&&/memory/'

Note: You can get rid of sed as well using NR variable in awk

Code:
awk 'NR>=2&&NR<=10&&/error/&&!/memory/{c++}END{print c}' lfile
awk 'NR>=2&&NR<=10&&/error/&&!/memory/' lfile
awk 'NR>=2&&NR<=10&&/error/&&/memory/{c++}END{print c}' lfile
awk 'NR>=2&&NR<=10&&/error/&&/memory/' lfile

The Following 2 Users Say Thank You to Yoda For This Useful Post:
MattyV (01-06-2013), SkySmart (01-06-2013)
Sponsored Links
    #3  
Old 01-06-2013
SkySmart SkySmart is offline
Registered User
 
Join Date: Dec 2006
Last Activity: 11 December 2014, 6:13 PM EST
Posts: 655
Thanks: 418
Thanked 2 Times in 2 Posts
Quote:
Originally Posted by bipinajith View Post
Code:
sed -n "2,10p" lfile | awk '/error/&&!/memory/{c++}END{print c}'
sed -n "2,10p" lfile | awk '/error/&&!/memory/'
sed -n "2,10p" lfile | awk '/error/&&/memory/{c++}END{print c}'
sed -n "2,10p" lfile | awk '/error/&&/memory/'

Note: You can get rid of sed as well using NR variable in awk

Code:
awk 'NR>=2&&NR<=10&&/error/&&!/memory/{c++}END{print c}' lfile
awk 'NR>=2&&NR<=10&&/error/&&!/memory/' lfile
awk 'NR>=2&&NR<=10&&/error/&&/memory/{c++}END{print c}' lfile
awk 'NR>=2&&NR<=10&&/error/&&/memory/' lfile


thank you1!!!

will this work if i use egrep type techiques?

like


Code:
sed -n "2,9p" lfile | egrep "errror.*skysmart.net" | egrep -v "memory|panic|error|failure.*2:00pm"

see what i did there. basically, will awk reliably understand and process the ".*" and "|" like egrep would?

and by the way, when i run the commands that's suppose to give a count, if nothing matches, it just returns a blank. anyway to make it return a 0 instead when nothing is found that matches the specified strings?

Last edited by SkySmart; 01-06-2013 at 02:03 PM..
    #4  
Old 01-06-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 19 December 2014, 6:33 PM EST
Location: Galactic Empire
Posts: 3,395
Thanks: 240
Thanked 1,211 Times in 1,137 Posts
To make it return 0 intialize variable value to 0:

Code:
awk 'BEGIN{c=0}NR>=2&&NR<=10&&/error/&&!/memory/{c++}END{print c}' lfile

awk is glorified variant of grep . So yes it understands regex.
The Following User Says Thank You to Yoda For This Useful Post:
SkySmart (01-06-2013)
Sponsored Links
    #5  
Old 01-06-2013
SkySmart SkySmart is offline
Registered User
 
Join Date: Dec 2006
Last Activity: 11 December 2014, 6:13 PM EST
Posts: 655
Thanks: 418
Thanked 2 Times in 2 Posts
thanks again. last question.


Code:
awk 'NR>='${NUMA}'&&NR<='${NUMB}'&&/'${STRING1}'/&&/'${STRING2}'/{c++}END{print c}' lfile

when i run the above from my bash script, it returns a:


Code:
awk: NR>=29018&&NR<=29027&&/SERVICE
awk:                        ^ unterminated regexp


Code:
NUMA=29018
NUMB=29027
STRING1='SERVICE NOTIFICATION'
STRING2='CRITICAL'

what am i doing wrong?

Last edited by Scrutinizer; 01-06-2013 at 02:38 PM.. Reason: code tags
Sponsored Links
    #6  
Old 01-06-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 20 December 2014, 8:16 AM EST
Location: Amsterdam
Posts: 9,691
Thanks: 300
Thanked 2,491 Times in 2,230 Posts
I do not really see what this all brings, I think I would prefer the two greps to the awk solutions, because they are easier to understand and thus to maintain. One could go even further and put everything in one awk:


Code:
awk '
NR==2,NR==10 {
  if (/error/) {
    if(/memory/) {
      m=m RS $0
      i++
    } 
    else {
      o=o RS $0
      j++ 
    }
  }
} 
END{
  print i m RS j o 
}
' file

This probably would make it more efficient, but one could ask if that is required and the down side is that it is also less simple...


--
Quote:
Originally Posted by bipinajith View Post
[..]
awk is glorified variant of grep . So yes it understands regex.
Ehm.. I don't agree with that..
Sponsored Links
    #7  
Old 01-06-2013
SkySmart SkySmart is offline
Registered User
 
Join Date: Dec 2006
Last Activity: 11 December 2014, 6:13 PM EST
Posts: 655
Thanks: 418
Thanked 2 Times in 2 Posts
Quote:
Originally Posted by Scrutinizer View Post
I do not really see what this all brings, I think I would prefer the two greps to the awk solutions, because they are easier to understand and thus to maintain. One could go even further and put everything in one awk:


Code:
awk '
NR==2,NR==10 {
  if (/error/) {
    if(/memory/) {
      m=m RS $0
      i++
    } 
    else {
      o=o RS $0
      j++ 
    }
  }
} 
END{
  print i m RS j o 
}
' file

This probably would make it more efficient, but one could ask if that is required and the down side is that it is also less simple...


--


Ehm.. I don't agree with that..

thank you!

the thing is, i have a very huge script that uses quite a lot of egreps like those in my first post. however, while the script runs beautifully, i notice there are just too many commands being called in it. commands that, if given thought can be combined into one by a more advanced user. which is why i created this thread.

bipinajith's one-liner solution not only helps to combine the egreps in my post, it also got rid of the sed. so in essence, his awk did what i (not an advanced user) was trying to do with 3 commands (sed, egrep, egrep).

i'm making the modifcations to my script as we speak. and i'll see if the awk solution helps to decrease the script's overall processing time.

btw, can you please combine your awk suggestion into a one-liner? i think its easier to read for a novice if anywhere is in one line. if you can, please explain the code for me.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
AWK/GREP: grep only lines starting with integer chrisjorg Shell Programming and Scripting 2 04-17-2012 11:06 AM
translate sed to awk adam25bc Shell Programming and Scripting 14 12-06-2011 07:44 PM
Read content between xml tags with awk, grep, awk or what ever... Sebi0815 Shell Programming and Scripting 5 03-10-2010 06:58 PM
Is it better to grep and pipe to awk, or to seach with awk itself DeCoTwc Shell Programming and Scripting 4 10-07-2008 03:52 PM
MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else hariza Shell Programming and Scripting 4 09-18-2008 03:56 AM



All times are GMT -4. The time now is 09:37 AM.