removing lines around a matched pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting removing lines around a matched pattern
# 1  
Old 11-10-2010
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..
Code:
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...

Last edited by tamale; 11-10-2010 at 01:04 PM..
# 2  
Old 11-10-2010
could you post some part of your conf file?
# 3  
Old 11-10-2010
sure..
Code:
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

Code:
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:
Code:
grep -2 "PATTERN" INFILE | diff - INFILE | sed '/^> /!d;s/^> //'

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

Last edited by Scott; 11-10-2010 at 02:13 PM..
This User Gave Thanks to tamale For This Post:
# 4  
Old 11-10-2010
Thanks for sharing your discovery! So solved then?
# 5  
Old 11-10-2010
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.
# 6  
Old 11-10-2010
use this site's 'Search' capability for similar threads - lookie here as an example.
# 7  
Old 11-10-2010
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:
stackoverflow.com/questions/876446/how-do-i-delete-a-matching-line-the-line-above-and-the-one-below-it-using-sed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comment all lines which are not already commented for each full path pattern matched

Hello. Question 1 : I want to comment out all lines of a cron file which are not already commented out for each full path pattern matched. Example 1 nothing to do because line is already commented out; pattern = '/usr/bin/munin-cron' # */5 * * * * munin test -x... (3 Replies)
Discussion started by: jcdole
3 Replies

2. UNIX for Dummies Questions & Answers

Removing PATTERN from txt without removing lines and general text formatting

Hi Everybody! First post! Totally noobie. I'm using the terminal to read a poorly formatted book. The text file contains, in the middle of paragraphs, hyphenation to split words that are supposed to be on multiple pages. It looks ve -- ry much like this. I was hoping to use grep -v " -- "... (5 Replies)
Discussion started by: AxeHandle
5 Replies

3. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

4. Shell Programming and Scripting

Delete lines and the first pattern between 2 matched patterns

Hi, i need help to delete all the lines between 2 matched patterns and the first pattern must be deleted too. sample as follows: inputfile.txt >kump_1 ........................... ........................... >start_0124 dgfhghgfh fgfdgfh fdgfdh >kump_2 ............................. (7 Replies)
Discussion started by: redse171
7 Replies

5. Linux

Perl program to print previous set of lines once a pattern is matched

Hi all, I have a text data file. My aim here is to find line called *FIELD* AV for every record and print lines after that till *FIELD* RF. But here I want first 3 to four lines for very record as well. FIELD AV is some where in between for very record. SO I am not sure how to retrieve lines in... (2 Replies)
Discussion started by: kaav06
2 Replies

6. Shell Programming and Scripting

Grep command to return all the lines between one matched pattern to another.

14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. 14:50:00- abcdefghijkl. 123456789. 15:30:00-abcdefghijkl. (3 Replies)
Discussion started by: dev_shivv
3 Replies

7. Solaris

Grep command to return all the lines from one matched pattern to another.

For example a log file looks like below- 13:30:00- abcdefghijklhjghjghjhskj. abcdefghijkl. 14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. (0 Replies)
Discussion started by: dev_shivv
0 Replies

8. Shell Programming and Scripting

How to print lines till till a pattern is matched in loop

Dear All I have a file like this 112534554 446538656 444695656 225696966 226569744 228787874 113536566 443533535 222564552 115464656 225445345 225533234 I want to cut the file into different parts where the first two columns are '11' . The first two columns will be either... (3 Replies)
Discussion started by: anoopvraj
3 Replies

9. Shell Programming and Scripting

removing pattern which is spread in multiple lines

I have several huge files wich contains oracle table creation scripts as follows: I would need to remove the pattern colored in red above. Any sed/awk/pearl code will be of much help. Thanks (2 Replies)
Discussion started by: sabyasm
2 Replies

10. Shell Programming and Scripting

SED: match pattern & delete matched lines

Hi all, I have the following data in a file x.csv: > ,this is some text here > ,,,,,,,,,,,,,,,,2006/11/16,0.23 > ,,,,,,,,,,,,,,,,2006/12/16,0.88 < ,,,,,,,,,,,,,,,,this shouldnt be deleted I need to use SED to match anything with a > in the line and delete that line, can someone help... (7 Replies)
Discussion started by: not4google
7 Replies
Login or Register to Ask a Question