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 10-12-2007, 01:10 PM   #1
new_2_unix
LQ Newbie
 
Registered: Oct 2007
Posts: 26

Rep: Reputation: 15
how to use sed's print function after a /d


hi,

i have a file 'myFile' with the contents:

Line one.
Second.
The third.

what i have to do is use sed to delete from line 1 to the line that contains 'Second'.

i can do this with the following which works just as expected:
$ cat myFile | sed 1,/'Second'/d

however, my problem is that i have to use sed with a -n option for other reasons, so it looks like:
$ cat myFile | sed -n 1,/'Second'/d

This does not print the output because of the -n option.

if i append /p to it:
$ cat myFile | sed 1,/'Second'/d/p

it results in error. this is probably a small syntax thing i'm missing, but i can't figure out what it is. i've tried the /p thing many ways, but it just doesn't work.

any ideas where i may be wrong? thanks!!
 
Old 10-12-2007, 01:21 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

You could turn things around:

deleting line 1 to the Second line is the same as printing Second line to end.

sed -n '/Second/,$p' myFile

Testrun:
Code:
$ cat myFile 
Line one.
Second.
The third.

$ sed -n '/Second/,$p' myFile 
Second.
The third.
In case you did not know: $ points to the last line.

Why must you use the -n option?

Hope this helps.
 
Old 10-12-2007, 01:46 PM   #3
new_2_unix
LQ Newbie
 
Registered: Oct 2007
Posts: 26

Original Poster
Rep: Reputation: 15
hi druuna,

thanks. that approach helps me... there's just one slight thing extra that i was trying to do with my previous approach, and that is not to print the line 'Second'. is there a way to do that?

i'm using -n because i'm writing a sed script file that will receive a very large file as an input, i want to print only sections of the file.

thanks again for your help!
 
Old 10-12-2007, 01:58 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
I wold recommend forming the habit of enclosing the whole sed program string in single quotes. They you can use spaces within it and very much improve readability. Multiple commands can be separated with semi-colons:
Code:
cat myFile | sed -n '1,/Second/ d ; p'
 
Old 10-12-2007, 02:08 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

My first thought was: sed -n '/The Third/,$p' myFile. But I doubt that the myFile example given in your first post is accurate.

Maybe you could post a relevant part of the myFile (replacing possible privacy sensitive stuff) and give us a larger view of what it is you are trying to do.

About the -n option: As you probably found out the -n (suppress normal output) and p (print current pattern space) option kinda go together and is probably what you want to use to cut out parts of the large infile.

Hope this helps.
 
Old 10-12-2007, 02:36 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
You do you need to use the -n option?

I you just want to then you can use
sed -n '1,/The Third/!p' source >destination

This will be the same as
sed '1,/The Third/d' source >destination

If your sed program is working and debugged you can edit the source file insitu:
sed -i '1,/The Third/d' source

You might want to do this is you have a very massive file and don't have room to create a temp file for the output.

Last edited by jschiwal; 10-12-2007 at 02:38 PM.
 
Old 10-12-2007, 03:30 PM   #7
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 728

Rep: Reputation: 74
Hi, jschiwal (neighbor on my west).
Quote:
Originally Posted by jschiwal View Post
... You might want to do this is you have a very massive file and don't have room to create a temp file for the output.
One needs to be careful here if one is cramped for space: my version of sed will create the temporary file itself if this option is specified:
Quote:
`-i[SUFFIX]'
`--in-place[=SUFFIX]'
This option specifies that files are to be edited in-place. GNU
`sed' does this by creating a temporary file and sending output to
this file rather than to the standard output.(1) ...

-- excerpt from info sed
The in-place term is something of a misnomer here, because it refers to the final result, not the mode of operation ... cheers, makyo
 
Old 10-12-2007, 04:13 PM   #8
new_2_unix
LQ Newbie
 
Registered: Oct 2007
Posts: 26

Original Poster
Rep: Reputation: 15
hi all,

thank u for all the input. i think in my case i was messing up the syntax, and the following solved it:

> sed -n '1,/Second/d;p'

all of the information in this thread has been really helpful. once again, thanks everyone!
 
  


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
Need help filtering out sed's "special" characters fatsheep Programming 2 11-09-2006 04:54 PM
Print-to-file print driver to print PDF Bill Fox Linux - General 3 05-02-2006 04:15 PM
Print function has gone Poof jhimes Linux - General 4 09-03-2005 05:07 PM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM


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