Skip to main content
fix extra empty line when the first line matches (thanks Stéphane Chazelas)
Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k

If you have GNU grep, you can use Perl regular expressions, which have a negation construct.

grep -A1 -P '^(?!.*ignore me).*needle' 

If you don't have GNU grep, you can emulate its before/after context options in awk.

awk -v after=1 -v before=0 ' start <= until { start = until + 1; } /needle/ && !/ignore me/ { for (i = start; i < NR; i++) { print h[i]; delete h[i]; } until = NR + after; start = until; } { if (NR <= until) print $0; else h[NR] = $0; start = NR - before + 1; delete h[start-1]; if (start <= until) start = until + 1; } END {exit !until} ' 

If you have GNU grep, you can use Perl regular expressions, which have a negation construct.

grep -A1 -P '^(?!.*ignore me).*needle' 

If you don't have GNU grep, you can emulate its before/after context options in awk.

awk -v after=1 -v before=0 ' /needle/ && !/ignore me/ { for (i = start; i < NR; i++) { print h[i]; delete h[i]; } until = NR + after; start = until; } { if (NR <= until) print $0; else h[NR] = $0; start = NR - before + 1; delete h[start-1]; if (start <= until) start = until + 1; } END {exit !until} ' 

If you have GNU grep, you can use Perl regular expressions, which have a negation construct.

grep -A1 -P '^(?!.*ignore me).*needle' 

If you don't have GNU grep, you can emulate its before/after context options in awk.

awk -v after=1 -v before=0 ' start <= until { start = until + 1; } /needle/ && !/ignore me/ { for (i = start; i < NR; i++) { print h[i]; delete h[i]; } until = NR + after; start = until; } { if (NR <= until) print $0; else h[NR] = $0; start = NR - before + 1; delete h[start-1]; } END {exit !until} ' 
fix awk solution that printed before lines out of order when before > 1 (thanks Stéphane Chazelas)
Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k

If you have GNU grep, you can use Perl regular expressions, which have a negation construct.

grep -A1 -P '^(?!.*ignore me).*needle' 

If you don't have GNU grep, you can emulate its before/after context options in awk.

awk -v after=3after=1 -v before=2before=0 ' /needle/ && !/ignore me/ { for (i in= hstart; i < NR; i++) { print h[i]; delete h[i]; } until = NR + after; start = until; } { if (NR <= until) print $0; else h[NR] = $0; start = NR - before + 1; delete h[NRh[start-before];1]; if (start <= until) start = until + 1; } END {exit !until} ' 

If you have GNU grep, you can use Perl regular expressions, which have a negation construct.

grep -A1 -P '^(?!.*ignore me).*needle' 

If you don't have GNU grep, you can emulate its before/after context options in awk.

awk -v after=3 -v before=2 ' /needle/ && !/ignore me/ { for (i in h) { print h[i]; delete h[i]; } until = NR + after; } { if (NR <= until) print $0; else h[NR] = $0; delete h[NR-before]; } END {exit !until} ' 

If you have GNU grep, you can use Perl regular expressions, which have a negation construct.

grep -A1 -P '^(?!.*ignore me).*needle' 

If you don't have GNU grep, you can emulate its before/after context options in awk.

awk -v after=1 -v before=0 ' /needle/ && !/ignore me/ { for (i = start; i < NR; i++) { print h[i]; delete h[i]; } until = NR + after; start = until; } { if (NR <= until) print $0; else h[NR] = $0; start = NR - before + 1; delete h[start-1]; if (start <= until) start = until + 1; } END {exit !until} ' 
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

If you have GNU grep, you can use Perl regular expressions, which have a negation construct.

grep -A1 -P '^(?!.*ignore me).*needle' 

If you don't have GNU grep, you can emulate its before/after context options in awkemulate its before/after context options in awk.

awk -v after=3 -v before=2 ' /needle/ && !/ignore me/ { for (i in h) { print h[i]; delete h[i]; } until = NR + after; } { if (NR <= until) print $0; else h[NR] = $0; delete h[NR-before]; } END {exit !until} ' 

If you have GNU grep, you can use Perl regular expressions, which have a negation construct.

grep -A1 -P '^(?!.*ignore me).*needle' 

If you don't have GNU grep, you can emulate its before/after context options in awk.

awk -v after=3 -v before=2 ' /needle/ && !/ignore me/ { for (i in h) { print h[i]; delete h[i]; } until = NR + after; } { if (NR <= until) print $0; else h[NR] = $0; delete h[NR-before]; } END {exit !until} ' 

If you have GNU grep, you can use Perl regular expressions, which have a negation construct.

grep -A1 -P '^(?!.*ignore me).*needle' 

If you don't have GNU grep, you can emulate its before/after context options in awk.

awk -v after=3 -v before=2 ' /needle/ && !/ignore me/ { for (i in h) { print h[i]; delete h[i]; } until = NR + after; } { if (NR <= until) print $0; else h[NR] = $0; delete h[NR-before]; } END {exit !until} ' 
Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k
Loading