Translate grep to awk

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:

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

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

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
2 Likes

thank you1!!!

will this work if i use egrep type techiques?

like

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?

To make it return 0 intialize variable value to 0:

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.

1 Like

thanks again. last question.

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

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

awk: NR>=29018&&NR<=29027&&/SERVICE
awk:                        ^ unterminated regexp
NUMA=29018
NUMB=29027
STRING1='SERVICE NOTIFICATION'
STRING2='CRITICAL'

what am i doing wrong?

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:

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.. :):confused:

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.

On one line it would look like this, but that would be a bit long:

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