LinuxQuestions.org
Help answer threads with 0 replies.
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 12-13-2012, 05:43 AM   #1
rozz.alex
LQ Newbie
 
Registered: Dec 2012
Posts: 3

Rep: Reputation: Disabled
CUT or SED


Hi All,

I have a file as below,

REM<1><2><3><4><5>
some text1
some text2
some text3
some text4
some text5
REM<1><2><3><4><5>
some text6
some text7
some text8
some text9
some text0

in which I need to cut the text above "REM<1><2><3><4><5>" (2nd one). Is it possible to do with cut/sed?

Thanks,
rozz
 
Old 12-13-2012, 06:00 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Hi and welcome to LQ.

Try the following:

Code:
sed -n 'H; /^REM/h; ${g;p;}' input_file
 
3 members found this post helpful.
Old 12-13-2012, 06:18 AM   #3
rozz.alex
LQ Newbie
 
Registered: Dec 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hi Sycamorex,

Thanks a lot... It works!.
Can you please explain what the code actually does?

Thanks,
rozz
 
Old 12-13-2012, 07:22 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272Reputation: 2272
see man sed: http://linux.die.net/man/1/sed
H will append pattern space to hold space.
/^REM/h will copy lines (beginning with REM) to hold space
$ is an address, the last line
{ } is a block executed at $
g copy hold space to pattern space
p prints the current pattern space

So /^REM/h cleans up the hold space and copies the line into it (only when line begins with REM). Everything before the REM line will be lost.
H will append the actual line to the hold space
and ${g;p;} will at the end of the file copy the appended lines to the pattern space and print it
 
1 members found this post helpful.
Old 12-13-2012, 06:11 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Also, apart from man pages, a tutorial that is an absolute must when it comes to sed can be found here.
 
1 members found this post helpful.
Old 12-18-2012, 07:11 AM   #6
rozz.alex
LQ Newbie
 
Registered: Dec 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank u... It is very informative.
 
  


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
using grep, awk or sed to find and cut data Jekikii Programming 8 04-21-2011 10:48 AM
sed/awk/cut/grep.. looking for the best solution voda87 Programming 4 03-03-2011 08:59 AM
how can i use sed to cut out all the text up until the pattern? daweefolk Linux - Newbie 4 02-15-2011 10:17 AM
using SED or AWK to cut data from a file, between certain characters hugh86 Linux - Newbie 18 10-28-2010 05:17 AM
Piping output fm cut into sed wtaicken Programming 7 12-09-2008 11:54 AM


All times are GMT -5. The time now is 11:54 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration