|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 07:52 AM.. |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
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 |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Quote:
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 01:03 PM.. |
|
#4
|
||||
|
||||
|
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}' lfileawk 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
|
|||
|
|||
|
thanks again. last question. Code:
awk 'NR>='${NUMA}'&&NR<='${NUMB}'&&/'${STRING1}'/&&/'${STRING2}'/{c++}END{print c}' lfilewhen 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 01:38 PM.. Reason: code tags |
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
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
}
' fileThis 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:
![]()
|
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
Quote:
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 10:06 AM |
| translate sed to awk | adam25bc | Shell Programming and Scripting | 14 | 12-06-2011 06:44 PM |
| Read content between xml tags with awk, grep, awk or what ever... | Sebi0815 | Shell Programming and Scripting | 5 | 03-10-2010 05: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 02: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 02:56 AM |
|
|