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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
10-12-2007, 02:10 PM
|
#1
|
LQ Newbie
Registered: Oct 2007
Posts: 26
Rep:
|
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!!
|
|
|
10-12-2007, 02:21 PM
|
#2
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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.
|
|
|
10-12-2007, 02:46 PM
|
#3
|
LQ Newbie
Registered: Oct 2007
Posts: 26
Original Poster
Rep:
|
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!
|
|
|
10-12-2007, 02:58 PM
|
#4
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
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'
|
|
|
10-12-2007, 03:08 PM
|
#5
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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.
|
|
|
10-12-2007, 03:36 PM
|
#6
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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 03:38 PM.
|
|
|
10-12-2007, 04:30 PM
|
#7
|
Member
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735
Rep:
|
Hi, jschiwal (neighbor on my west).
Quote:
Originally Posted by jschiwal
... 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
|
|
|
10-12-2007, 05:13 PM
|
#8
|
LQ Newbie
Registered: Oct 2007
Posts: 26
Original Poster
Rep:
|
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!
|
|
|
All times are GMT -5. The time now is 04:39 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|