Translate grep to awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Translate grep to awk
# 1  
Old 01-06-2013
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..
# 2  
Old 01-06-2013
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

These 2 Users Gave Thanks to Yoda For This Post:
# 3  
Old 01-06-2013
Quote:
Originally Posted by bipinajith
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
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.
This User Gave Thanks to Yoda For This Post:
# 5  
Old 01-06-2013
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
# 6  
Old 01-06-2013
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
[..]
awk is glorified variant of grep. So yes it understands regex.
Ehm.. I don't agree with that.. SmilieSmilie
# 7  
Old 01-06-2013
Quote:
Originally Posted by Scrutinizer
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.. SmilieSmilie

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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Translate bash mathematical calculation to awk

this code below is very useful in calculating mean and quartiles. however, i would really like to translate it to awk without having to write to any external file: #!/bin/sh filename="tmp.txt" sort -n $1 >$filename rows=`wc -l $filename|cut -d' ' -f1` q2=`echo "($rows+1)/2" |bc` ... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. UNIX for Dummies Questions & Answers

Piping grep into awk, read the next line using grep

Hi, I have a number of files containing the information below. """"" Fundallinfo 6.3950 14.9715 14.0482 """"" I would like to grep for Fundallinfo and use it to read the next line? I ideally would like to read the three numbers that follow in the next line and... (2 Replies)
Discussion started by: Paul Moghadam
2 Replies

3. Shell Programming and Scripting

AWK/GREP: grep only lines starting with integer

I have an input file 12.4 1.72849432773174e+01 -7.74784188610632e+01 12.5 9.59432114416327e-01 -7.87018212757537e+01 15.6 5.20139995965960e-01 -5.61612429666624e+01 29.3 3.76696387248366e+00 -7.42896194101892e+01 32.1 1.86899877018077e+01 -7.56508762501408e+01 35 6.98857157014640e+00... (2 Replies)
Discussion started by: chrisjorg
2 Replies

4. Shell Programming and Scripting

translate sed to awk

hi, can someone tell me how can I translate the following line from sed to awk? `sed 's/^*:*:*:*:*:\(*\):.*/\1/ How to use code tags when posting data and code samples. (14 Replies)
Discussion started by: adam25bc
14 Replies

5. Shell Programming and Scripting

translate this string

Hi, I'm using code below to assign title for Putty Window to show user@hostname+curr directory. Trying to read this "write only" language, can anybody help me to go thru this string, too many thing in it I can't expaing Tx T export PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}:... (1 Reply)
Discussion started by: trento17
1 Replies

6. UNIX for Dummies Questions & Answers

Help translate code

Hi, all of you!!! I have this code and I won to build-in some more code. I know this is a lot of code. if ; then if ; then SYSROOT="$1" S_SCRIPT="cd $1 ; ./etc/rc.sysinit 2>&1 &" fi else ... (22 Replies)
Discussion started by: jokerper
22 Replies

7. Shell Programming and Scripting

Read content between xml tags with awk, grep, awk or what ever...

Hello, I trying to extract text that is surrounded by xml-tags. I tried this cat tst.xml | egrep "<SERVER>.*</SERVER>" |sed -e "s/<SERVER>\(.*\)<\/SERVER>/\1/"|tr "|" " " which works perfect, if the start-tag and the end-tag are in the same line, e.g.: <tag1>Hello Linux-Users</tag1> ... (5 Replies)
Discussion started by: Sebi0815
5 Replies

8. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies

9. Shell Programming and Scripting

translate rc to string

Hi Maybe you can suggest a nicer way to do the following: RET_STR=$(echo ${RET} | sed -e 's/0/Object is not mapped/' \ -e 's/1/Operation Internal Error/' \ -e 's/2/Operation Invalid Arguments/' \ -e 's/3/Object is mapped/' \ -e 's/4/Path not found/') (3 Replies)
Discussion started by: ynir
3 Replies

10. Shell Programming and Scripting

Can someone help translate this snippet?

Hello all - This snippet from a script runs on a Tru64 machine (ksh). if ps -ef | grep thing1 | grep dtsession | grep -v grep then echo "Killing Thing1 desktop session" kill -9 'ps -ef | grep thing1 | grep dtsession | grep -v grep | awk '{FS = " "}{print $2}'' fi I'm... (7 Replies)
Discussion started by: Heron
7 Replies
Login or Register to Ask a Question