LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-21-2008, 01:24 PM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 205

Rep: Reputation: 30
Delete region of lines with sed


Hi,

How can I delete a region of lines after the matching pattern?
Example, I have the folowing lines

#Superfiction
Superman = Man
batman = Man
Cinderella = Woman

I want when the pattern is matched at "Superfiction", I want all the next three lines including the pattern to be removed.

I've tried:
sed '/Suerfiction/1,3d'

But it doesn't work.
Thanks,
Indy
 
Old 09-21-2008, 01:43 PM   #2
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 330

Rep: Reputation: 140Reputation: 140
Using GNU sed:-
Code:
sed '/Superfiction/,+3d'

Last edited by Kenhelm; 09-21-2008 at 01:54 PM. Reason: Spelling
 
Old 09-21-2008, 02:04 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
I've tried:
sed '/Suerfiction/1,3d'

But it doesn't work.
What syntax were you assuming and what happened? I don't think ranges in SED can be stated in terms of offset from current position, eg, this works:

sed '1,/pattern/ <action>'

But this:

sed '/pattern/,3' <action>'
performs <action> starting with the line containing <pattern>, thru the 3rd line from the beginning


Try this:
sed '/Superfiction/,/^$/d' lines

(removes everything from the line containing "Superfiction" to the first empty line.)
 
Old 09-21-2008, 02:11 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Well---it appears I spoke too soon.

My favorite SED reference (http://www.grymoire.com/Unix/Sed.html) does not cover relative line# addressing. Is this unique to GNU SED?

(I know, I should go look in ABS.....)
 
Old 09-21-2008, 02:53 PM   #5
freeindy
Member
 
Registered: Nov 2002
Posts: 205

Original Poster
Rep: Reputation: 30
nice, thanks.



Indy
 
Old 09-21-2008, 03:37 PM   #6
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 330

Rep: Reputation: 140Reputation: 140
@pixellany
GNU sed has these extensions, but I don't know if they are unique to it:-
addr1,+N Matches addr1 and the N lines following addr1.
addr1,~N Matches addr1 and the lines following addr1 until the next line whose input line number is a multiple of N.
They are mentioned here:-
http://www.gnu.org/software/sed/manu...l#sed-Programs
 
Old 09-21-2008, 04:45 PM   #7
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Or with any sed:
sed -n '/pattern/ {n; n; n; d}; p'

Yves.
 
Old 09-21-2008, 05:12 PM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
It is good practice to use blank lines to separate each record. Then 'sed '/pattern/,/^$/d' will do it. It also allows easy extraction, and you can easily read the records into awk as well by simply using RS="".

Look at the output of lspci -v.
e.g.:
Code:
/sbin/lspci -v | sed -n '/Wireless/,/^$/p'
 
  


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
sed or grep : delete lines containing matching text raj000 Linux - General 18 09-08-2012 10:38 AM
Sed: how to delete all lines except those matching........ DX398 Programming 9 07-23-2008 03:39 AM
delete lines with sed LuciusHunt Programming 1 04-19-2008 03:40 PM
Insert and delete lines at the end of a file using sed DriveMeCrazy Programming 1 01-05-2007 02:45 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


All times are GMT -5. The time now is 04:11 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration