LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 06-09-2012, 05:42 AM   #1
vk2012
LQ Newbie
 
Registered: Jun 2012
Posts: 2

Rep: Reputation: Disabled
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.
 
Old 06-09-2012, 06:58 AM   #2
vk2012
LQ Newbie
 
Registered: Jun 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
This is solved.

Quote:
awk '/pattern1/{f=1} f && /pattern2/{c=1; print} c && /pattern3/{c=0; f=0} !c' file
 
Old 06-09-2012, 07:16 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
Old 06-10-2012, 07:18 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
Old 06-10-2012, 10:03 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.)
 
Old 06-10-2012, 10:34 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by David the H. View Post
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.
 
Old 06-10-2012, 12:02 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] SED/AWK - Delete all lines until empty line is found after pattern match vikas027 Programming 13 03-28-2012 09:33 AM
[SOLVED] sed print range from pattern1 to pattern2 porphyry5 Programming 7 06-15-2011 10:57 AM
sed/awk: Three consecutive blank lines in a file, how to delete two of them? recomboDNA Programming 8 06-17-2010 10:50 AM
Delete specific Range of lines Using sed , awk, grep etc. joyds219 Linux - Newbie 4 03-28-2008 09:59 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 10:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration