LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Delete Lines : after pattern1 and between pattern2 and patter3 using awk/sed/perl (https://www.linuxquestions.org/questions/linux-newbie-8/delete-lines-after-pattern1-and-between-pattern2-and-patter3-using-awk-sed-perl-949337/)

vk2012 06-09-2012 04:42 AM

Delete Lines : after pattern1 and between pattern2 and patter3 using awk/sed/perl
 
Hi

I need to delete lines from a file which are after pattern1 and between pattern 2 and patter3, as below:

aaaaaaaa
bbbbbbbb
pattern1
cdededed
ddededed
pattern2
fefefefe <-----Delete this line
efefefef <-----Delete this line
pattern3
adsffdsd
huaserew

Please can you suggest how this can be done using awk or sed or in perl.

Thank you.

vk2012 06-09-2012 05:58 AM

This is solved.

Quote:

awk '/pattern1/{f=1} f && /pattern2/{c=1; print} c && /pattern3/{c=0; f=0} !c' file

David the H. 06-09-2012 06:16 AM

Can we assume that each pattern only appears once in the input, or that the structure of the three patterns repeats itself regularly?

If we can, then here's a sed solution:

Code:

sed -r '/pattern1/,/pattern3/ { /pattern2/,/pattern3/ { /(pattern2|pattern3)/! d } }' infile
It first matches a section from pattern1 to pattern3, then from that selection, it matches all lines from pattern2 to pattern3. Finally from that selection, it deletes everything except the two pattern lines.

pixellany 06-10-2012 06:18 AM

Another solution:
Code:

sed -n '/patt1/, $ {/patt2/{:1 n; /patt3/ q; p; b1}}' filename
Translation:
Search from "patt1" to the end of the file. When "patt2" is found, then go to the next line and start looking for "patt3". If found, then quit----otherwise, print the line and continue looking.

David the H. 06-10-2012 09:03 AM

Yo, pix, your version does the opposite of the request. It prints the lines that should be deleted, and vice-versa.

(I like the technique used though, I'm going to have to remember that.)

pixellany 06-10-2012 09:34 AM

Quote:

Originally Posted by David the H. (Post 4700006)
Yo, pix, your version does the opposite of the request. It prints the lines that should be deleted, and vice-versa.

OOOOPS---sorry! I spent so much time fiddling with it that I forgot the question.////;)
This all goes back a few years when I took it as a challenge to solve things with SED----many people don't realize how it supports looping, branching, etc.

I'm not a programmer, but I like puzzles.

David the H. 06-10-2012 11:02 AM

I know the feeling. :D

And yeah, sed can do all that, but it sure isn't simple. It's really easy to get confused over the code flow, particularly when you start incorporating the hold buffer. I once spent a whole evening trying to figure out how to get it to iterate over various combinations of n lines at once, with overlapping selections (e.g. 1+2,2+3,3+4 etc), and I'm still not sure I fully succeeded.


Speaking of which, for the OP...
Here are a few useful sed references:

http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt


All times are GMT -5. The time now is 05:32 PM.