LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-14-2010, 07:26 PM   #1
C_Blade
LQ Newbie
 
Registered: Apr 2010
Posts: 7

Rep: Reputation: 0
How to use sed to delete all lines before the first match of a pattern?


I have to use sed with this problem, and sed is extremely complicated, they could probably teach a whole class on this entire function.

Here's an example to show what I need to do:

Code:
aaaaaaaaaaaaacoolbbbbbbbbb
ccccccccoolddddcool
sweetcoolawesome
sweetgreat
how would I say that every line before I reach the first match of "sweet" should be deleted so that the output will look like:

Code:
sweetcoolawesome
sweetgreat
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-14-2010, 08:02 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,986
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
man sed
/print
 
Old 04-15-2010, 01:29 AM   #3
geoff_f
Member
 
Registered: May 2003
Location: Canberra, Australia
Distribution: openSUSE 11.3
Posts: 445

Rep: Reputation: 31
This might be more useful than the man pages:

http://www.grymoire.com/Unix/Sed.html

Look under the section 'Ranges by patterns'.
 
Old 04-15-2010, 02:40 AM   #4
r-t
Member
 
Registered: Oct 2004
Location: Manchester UK
Distribution: Fedora 13 (Gnome user), Debian (on older second computer)
Posts: 46

Rep: Reputation: 15
sed -n '/sweet/,$p' file

http://www.computing.net/answers/uni...-one/3572.html
 
Old 04-15-2010, 02:48 AM   #5
bakdong
Member
 
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
If you put a little bit of explanation with your answer, assuming it's not just 'RTFM', which, let's face it, could answer 90% of posts here, we can all learn or refresh/reinforce our knowledge from it.


sed -n '/sweet/,$p' filename


-n don't print lines by default

/sweet/,$ is a restriction for the following command p, meaning 'only look from the first occurence of 'sweet' to '$' (the end of the file)

p print
 
Old 04-15-2010, 07:58 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Guys;
With homework problems, we normally avoid simply handing over the answer.

Quote:
I have to use sed with this problem, and sed is extremely complicated
Au contraire!! SED is one of the simplest utilities in the toolbelt, with very limited action options. What you may be referring to is the fact that SED can be used to generate totally incomprehensible statements/scripts---many of which could be done better with some other tool........Watch for the periodic "SED vs. AWK" skirmishes here at LQ.
 
Old 04-15-2010, 08:20 AM   #7
lbutler
LQ Newbie
 
Registered: Feb 2007
Location: Edinburgh, UK
Distribution: debian gnu/linux lenny
Posts: 6

Rep: Reputation: 0
Try,

sed -r -n -e '/PATTERN/,${p}' files

The flag -n means that lines that do not match are discarded;
-r means use extended regexps.

The lines from the first match of PATTERN to the end of file ($)
while be printed by the p command, otherwise the line is discarded
by default.


Leo
 
Old 04-15-2010, 09:40 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,562

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Or we could delete as requested:
Code:
sed '/sweet/,$!d' file
Or just cause awk was mentioned
Code:
awk '/sweet/{i++}i' file
 
1 members found this post helpful.
Old 05-01-2010, 01:06 AM   #9
rkski
Member
 
Registered: Jan 2009
Location: Canada
Distribution: CentOS 6.3, Fedora 17
Posts: 247

Rep: Reputation: 51
Hi grail

Could you please explain what's in bold. I'm trying to strengthen my awk skills.

Code:
awk '/sweet/{i++}i' file

TIA
 
Old 05-01-2010, 04:18 AM   #10
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,562

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
All variables in awk start with a value of 0, so until we first encounter sweet the 'i' at the end will be 0 so false therefore nothing to be done.
Once we find sweet and increase 'i' with '++', now 'i' is not zero (therefore true) and we do the default action which is to print the line
 
2 members found this post helpful.
  


Reply

Tags
before, delete, lines, match, pattern


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: Find pattern and delete 5 lines after it supersoni3 Programming 4 03-24-2010 07:00 AM
sed: delete lines after last occurrence of a pattern in a file zugvogel Programming 4 11-17-2009 01:49 AM
help extracting a matching pattern and next lines of match madvicious Programming 8 09-13-2009 01:01 AM
simple pattern match with awk, sed alenD Linux - Newbie 10 03-10-2008 02:31 PM
sed display line after pattern match inonzi_prowler Linux - Software 3 02-19-2007 01:47 PM


All times are GMT -5. The time now is 02:54 AM.

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