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?
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
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.
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.
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.