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 |
I found a hellofa one liner that is equivalent to greps -A -B stuff:
Code:
gawk '{if(c-->0)print}{a[NR]=$0}/pattern/{for(i=NR-5;i<=NR;i++){print a[i]};c=5}' filename |
Hi.
The following solution uses a ring buffer to store exactly B lines of previous context: Code:
$ seq 1 50 | awk -vB=3 -vA=3 '/15/{for(n=1;n<=B;n++) print u[(i+n)%B]; print; a=A; next} {i=(i+1)%B; u[i]=$0} a-->0' Note also that Code:
{if(c-->0)print} Code:
c-->0 EDIT: output will look strange if pattern occurs more often than each B lines. |
All times are GMT -5. The time now is 01:32 AM. |