Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
06-09-2012, 05:42 AM
|
#1
|
LQ Newbie
Registered: Jun 2012
Posts: 2
Rep:
|
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.
|
|
|
06-09-2012, 06:58 AM
|
#2
|
LQ Newbie
Registered: Jun 2012
Posts: 2
Original Poster
Rep:
|
This is solved.
Quote:
awk '/pattern1/{f=1} f && /pattern2/{c=1; print} c && /pattern3/{c=0; f=0} !c' file
|
|
|
|
06-09-2012, 07:16 AM
|
#3
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.
|
|
1 members found this post helpful.
|
06-10-2012, 07:18 AM
|
#4
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
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.
|
|
1 members found this post helpful.
|
06-10-2012, 10:03 AM
|
#5
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.)
|
|
|
06-10-2012, 10:34 AM
|
#6
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Quote:
Originally Posted by David the H.
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.
|
|
|
06-10-2012, 12:02 PM
|
#7
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
I know the feeling.
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 09:28 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|