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. |
This is solved.
Quote:
|
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 |
Another solution:
Code:
sed -n '/patt1/, $ {/patt2/{:1 n; /patt3/ q; p; b1}}' filename 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. |
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.) |
Quote:
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. |
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. |