LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to use sed's print function after a /d (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-use-seds-print-function-after-a-d-591348/)

new_2_unix 10-12-2007 01:10 PM

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!!

druuna 10-12-2007 01:21 PM

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.

new_2_unix 10-12-2007 01:46 PM

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!

matthewg42 10-12-2007 01:58 PM

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'

druuna 10-12-2007 02:08 PM

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.

jschiwal 10-12-2007 02:36 PM

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.

makyo 10-12-2007 03:30 PM

Hi, jschiwal (neighbor on my west).
Quote:

Originally Posted by jschiwal (Post 2922223)
... 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

new_2_unix 10-12-2007 04:13 PM

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 05:19 AM.