Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

GAWK/GREP Equivalent

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-23-2012
metallica1973 metallica1973 is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 16 April 2015, 9:48 AM EDT
Location: Washington D.C
Posts: 192
Thanks: 26
Thanked 2 Times in 2 Posts
GAWK/GREP Equivalent

What is GAWK equivalent to greps -B 5 -A 5?


Code:
zgrep -i "^oct 20" /var/log/syslog*|grep -iB 5 -A 5 'postfix\/pickup
/var/log/syslog.1.gz:Oct 20 01:55:01 elmo CROND[7682]: (mail) CMD (/usr/bin/python -S /usr/lib64/mailman/cron/gate_news)
/var/log/syslog.1.gz:Oct 20 02:00:01 elmo CROND[7701]: (mail) CMD (/usr/bin/python -S /usr/lib64/mailman/cron/gate_news)
/var/log/syslog.1.gz:Oct 20 02:00:02 elmo CROND[7704]: (root) CMD (/home/davider/scripts/backups/saintbk.sh full )
/var/log/syslog.1.gz:Oct 20 02:00:03 elmo kernel:  CIFS VFS: Error 0xfffffffb on cifs_get_inode_info in lookup of \backups\automated
/var/log/syslog.1.gz:Oct 20 02:00:03 elmo last message repeated 11 times
/var/log/syslog.1.gz:Oct 20 02:00:03 elmo postfix/pickup[7339]: 585F8E607F: uid=0 from=<root>
/var/log/syslog.1.gz:Oct 20 02:00:03 elmo postfix/cleanup[7716]: 585F8E607F: message-id=<20121020060003.585F8E607F@elmo.localdomain>
/var/log/syslog.1.gz:Oct 20 02:00:03 elmo postfix/qmgr[3925]: 585F8E607F: from=<root@elmo.localdomain>, size=1723, nrcpt=1 (queue active)
/var/log/syslog.1.gz:Oct 20 02:00:03 elmo postfix/local[7718]: 585F8E607F: to=<postfix@elmo.localdomain>, orig_to=<root>, relay=local, delay=0.32, delays=0.05/0.2/0/0.08, dsn=2.0.0, status=sent (delivered to mailbox)
/var/log/syslog.1.gz:Oct 20 02:00:03 elmo postfix/qmgr[3925]: 585F8E607F: removed
/var/log/syslog.1.gz:Oct 20 02:01:01 elmo CROND[7723]: (root) CMD (nice -n 19 run-parts --report /etc/cron.hourly)
--
/var/log/syslog.1.gz:Oct 20 03:55:01 elmo CROND[8208]: (mail) CMD (/usr/bin/python -S /usr/lib64/mailman/cron/gate_news)
/var/log/syslog.1.gz:Oct 20 04:00:01 elmo CROND[8227]: (mail) CMD (/usr/bin/python -S /usr/lib64/mailman/cron/gate_news)
/var/log/syslog.1.gz:Oct 20 04:00:01 elmo CROND[8230]: (root) CMD (/home/davider/scripts/backups/spacechk.sh)
/var/log/syslog.1.gz:Oct 20 04:00:02 elmo kernel:  CIFS VFS: Error 0xfffffffb on cifs_get_inode_info in lookup of \backups\automated
/var/log/syslog.1.gz:Oct 20 04:00:02 elmo last message repeated 4 times
/var/log/syslog.1.gz:Oct 20 04:00:02 elmo postfix/pickup[8202]: 0D62FE607F: uid=0 from=<root>
/var/log/syslog.1.gz:Oct 20 04:00:02 elmo postfix/cleanup[8235]: 0D62FE607F: message-id=<20121020080002.0D62FE607F@elmo.localdomain>
/var/log/syslog.1.gz:Oct 20 04:00:02 elmo postfix/qmgr[3925]: 0D62FE607F: from=<root@elmo.localdomain>, size=1018, nrcpt=1 (queue active)
/var/log/syslog.1.gz:Oct 20 04:00:02 elmo postfix/local[8237]: 0D62FE607F: to=<postfix@elmo.localdomain>, orig_to=<root>, relay=local, delay=0.09, delays=0.08/0/0/0, dsn=2.0.0, status=sent (delivered to mailbox)
/var/log/syslog.1.gz:Oct 20 04:00:02 elmo postfix/qmgr[3925]: 0D62FE607F: removed
/var/log/syslog.1.gz:Oct 20 04:01:01 elmo CROND[8243]: (root) CMD (nice -n 19 run-parts --report /etc/cron.hourly)

Once the occurrence is found, I want to see 5 lines before and after. Thanks
Sponsored Links
    #2  
Old Unix and Linux 10-23-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 7 November 2014, 10:50 AM EST
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 Posts
Quote:
Originally Posted by metallica1973 View Post
Once the occurrence is found, I want to see 5 lines before and after.

Code:
grep -C 5 "search" file

Sponsored Links
    #3  
Old Unix and Linux 10-23-2012
metallica1973 metallica1973 is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 16 April 2015, 9:48 AM EDT
Location: Washington D.C
Posts: 192
Thanks: 26
Thanked 2 Times in 2 Posts
Many Thanks PAMU,

But I want to know how to do this using GAWK.
    #4  
Old Unix and Linux 10-23-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 7 November 2014, 10:50 AM EST
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 Posts
Quote:
Originally Posted by metallica1973 View Post
Many Thanks PAMU,

But I want to know how to do this using GAWK.
I don't think you need gawk here..

any specific reason behind this..?
Sponsored Links
    #5  
Old Unix and Linux 10-23-2012
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 4 April 2015, 12:03 PM EDT
Posts: 3,060
Thanks: 34
Thanked 660 Times in 639 Posts

Code:
gawk '{if(c-->0)print}{a[NR]=$0}/pattern/{for(i=NR-5;i<=NR;i++){print a[i]};c=5}' filename

Sponsored Links
    #6  
Old Unix and Linux 10-23-2012
metallica1973 metallica1973 is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 16 April 2015, 9:48 AM EDT
Location: Washington D.C
Posts: 192
Thanks: 26
Thanked 2 Times in 2 Posts
Many thanks,

That is one hell of a line using GAWK.Thanks
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep -v equivalent in perl dynamax Shell Programming and Scripting 4 05-19-2011 08:57 AM
SED equivalent for grep -w -f with pattern having special characters novice_man Shell Programming and Scripting 1 02-14-2011 10:38 PM
awk (gawk) grep & columns frajer Shell Programming and Scripting 4 11-08-2010 06:58 AM
AIX equivalent to GNU grep's -B and -A [print lines after or before matching lines] slashdotweenie Shell Programming and Scripting 4 04-28-2010 11:52 AM
perl equivalent to grep -c popeye Shell Programming and Scripting 6 04-19-2008 01:57 AM



All times are GMT -4. The time now is 03:39 AM.