removing lines around a matched pattern

I have an ugly conf file that has the string I'm interested in searching for in the middle of a block of code that's relevant, and I'm trying to find a way to remove that entire block based on the matched line.

I've googled for this problem, and most people helping are only interested in removing one line above and/or one line below the match, but I have to remove two lines above AND two lines below.

I thought this would work but the sed command isn't matching..

cat INFILE | sed '/"`grep -C 2 MATCH INFILE`"/{d;}'

..even though the grep command does show the 5 lines I want removed perfectly.

Is there an easier way to use the line number of the matched string to remove the 5 lines in question? Why isn't this sed replacement working? Is there an easier way to do this with awk or just pure sed?

Thanks...

could you post some part of your conf file?

sure..

Option "A" =
    String S = "AA";
    String F = "MATCHME.ABC";
    String I = "";
;
Option "B" =
    String S = "BB";
    String F = "MATCHME-BCA";
    String I = "";
;
Option "A" =
    String S = "AA";
    String F = "MATCHME.RASD";
    String I = "";
;
Registry "B" =
    String S = "BB";
    String F = "MATCHME.ABCDA";
    String I = "";
;

etc..

---------- Post updated at 09:45 AM ---------- Previous update was at 09:22 AM ----------

I got it.. found on the grep mailing list someone expecting similar behavior when using both the -v and -C options in grep..

w w w.archivum.info/gnu.utils.bug/2005-11/00093/Re-Bug-in-grep-when-using-both--after-context-and--invert-match.html

Oni Studio <info@xxxxxx> wrote:
> Logically, i tried:
> grep -v -A4 'zone "domain.com' /etc/bind/zones > /etc/bind/zones.new
>
> This did not produce the desired effect.

Right.  This gives you eery line that does not match your pattern,
along with the following four lines.  Here's one way to get what you
want (untested):
grep -A4 'zone "domain.com' /etc/bind/zones |
diff - /etc/bind/zones |
sed '/^> /!d;s/^> //'


paul

for me, this worked nicely by using:

grep -2 "PATTERN" INFILE | diff - INFILE | sed '/^> /!d;s/^> //'

hope someone finds this useful.. seems like it should be easier than this.

1 Like

Thanks for sharing your discovery! So solved then?

hm.. I've found a solution, but I feel like there must be a more efficient way to do this.. the diff | sed method shoots everything back through std out with the a '>' lines being replaced with blank lines, right?

I wouldn't mind seeing a more 'pure' solution, if anyone has one.

use this site's 'Search' capability for similar threads - lookie here as an example.

I've already searched extensively on this subject, and the examples I've posted were the closest to what I'm looking for.. if you've seen more, then by all means please share.

close, but only one line before and after:
w w w.unix.com/shell-programming-scripting/144993-sed-find-pattern-delete-line-before-after.html

same problem:

I did share a link to a relevant thread in my previous post - it should answer your exact question.

$
$
$ cat f4
Option "A" =
String S = "AA";
String F = "MATCHME.ABC";
String I = "";
;
Option "B" =
String S = "BB";
String F = "MATCHME-BCA";
String I = "";
;
Option "A" =
String S = "AA";
String F = "MATCHME.RASD";
String I = "";
;
Registry "B" =
String S = "BB";
String F = "MATCHME.ABCDA";
String I = "";
;
$
$
$ # Remove the block that contains the string "MATCHME-BCA"
$
$ perl -lne 'BEGIN {undef $/} while(/(^(Option|Registry).*?)\n;\n/msg) {print "$1\n;" if $1 !~ /MATCHME-BCA/}' f4
Option "A" =
String S = "AA";
String F = "MATCHME.ABC";
String I = "";
;
Option "A" =
String S = "AA";
String F = "MATCHME.RASD";
String I = "";
;
Registry "B" =
String S = "BB";
String F = "MATCHME.ABCDA";
String I = "";
;
$
$
$

tyler_durden

your shared link showed how to display contextual lines, which I said I was already able to do properly with the grep -C or grep -2 options... not remove them.

---------- Post updated at 12:07 PM ---------- Previous update was at 12:07 PM ----------

nice! this is pretty clean and easy to read, as well.. thanks.