removing lines around a matched pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting removing lines around a matched pattern
# 8  
Old 11-10-2010
I did share a link to a relevant thread in my previous post - it should answer your exact question.
# 9  
Old 11-10-2010
Code:
$
$
$ 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
# 10  
Old 11-10-2010
Quote:
Originally Posted by vgersh99
I did share a link to a relevant thread in my previous post - it should answer your exact question.
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 ----------

Quote:
Originally Posted by durden_tyler
Code:
$
$
$ 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
nice! this is pretty clean and easy to read, as well.. thanks.
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