LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 04: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, 05:00 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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, 05: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, 06:22 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
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, 05:11 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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, 06:11 AM   #6
rozz.alex
LQ Newbie
 
Registered: Dec 2012
Posts: 3

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


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

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

All times are GMT -5. The time now is 08:43 PM.

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