sed -n

Hi,
I need a little help to understand this sed code below:

sed -n 1, \$p file1 > file2

This is part of shell program.
What does $p here mean?

Thanks a lot!

Hmmm... All I understand is that this is illegal syntax - you must have made a typo somewhere if the script works.

I think it must have been something like
sed -n '1,$ p' somefile
In which case, it'd print each line once (without -n it'd print each line twice - once because of standard behaviour, twice because you've asked it to).

-n suppresses the standard behaviour of printing the pattern space. This can be useful for only printing certain lines, for example, to print only lines that contain the word foo
sed -n '/foo/p' somefile

Cheers
ZB

This is legal syntax. You don't need to to quote everything. If the $ is quoted, the shell will leave it alone.

It might even be useful if we really stretch. Suppose you have a text file. If you do:
echo bogus\\c >> textfile

textfile will have an incomplete last line.

sed -n 1,\$p textfile > textfile2
will remove that incomplete last line. This is a bit different than "cat textfile > textfile2".

Well I said it was a stretch! :stuck_out_tongue:

Well; without the space between , and \ it is legal! :wink: I just copied/pasted the OP's code into both Linux (bash) and HP-UX (sh-posix) and it fails.

Cheers
ZB

It worked on Unix Solaris. Although I am trying to figure out the purpuse of this code.

Perderabo,
What does \\c mean here?

echo bogus\\c >> textfile

Thanks!

You're right about that space. I didn't notice it. BTW, as to the original question, 1,$p says to print all the lines. $ used as an address means the last line.

It mean to suppress the newline.

zazzybob
You are right . There is no space between 1,\$p.
My mistake.

Perderabo and zazzybob
Thanks for the help!!!