LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 12:24 PM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Rep: Reputation: 32
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, 12:43 PM   #2
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

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

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

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 01:11 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 01:53 PM   #5
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Original Poster
Rep: Reputation: 32
nice, thanks.



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

Rep: Reputation: 170Reputation: 170
@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, 03:45 PM   #7
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

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

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

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



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 09:38 AM
Sed: how to delete all lines except those matching........ DX398 Programming 9 07-23-2008 02:39 AM
delete lines with sed LuciusHunt Programming 1 04-19-2008 02:40 PM
Insert and delete lines at the end of a file using sed DriveMeCrazy Programming 1 01-05-2007 01:45 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:22 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