Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

sed -n

Shell Programming and Scripting


Tags
linux

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-15-2004
whatisthis's Unix or Linux Image
whatisthis whatisthis is offline
Registered User
 
Join Date: Aug 2004
Last Activity: 1 November 2006, 5:31 PM EST
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Unix or Linux Question 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!
Sponsored Links
    #2  
Old Unix and Linux 12-15-2004
zazzybob's Unix or Linux Image
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 25 Times in 23 Posts
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
Sponsored Links
    #3  
Old Unix and Linux 12-15-2004
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 27 April 2015, 10:26 AM EDT
Location: Ashburn, Virginia
Posts: 9,931
Thanks: 64
Thanked 449 Times in 264 Posts
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! Linux
    #4  
Old Unix and Linux 12-15-2004
zazzybob's Unix or Linux Image
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 25 Times in 23 Posts
Well; without the space between , and \ it is legal! Linux I just copied/pasted the OP's code into both Linux (bash) and HP-UX (sh-posix) and it fails.

Cheers
ZB
Sponsored Links
    #5  
Old Unix and Linux 12-15-2004
whatisthis's Unix or Linux Image
whatisthis whatisthis is offline
Registered User
 
Join Date: Aug 2004
Last Activity: 1 November 2006, 5:31 PM EST
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
sed -n $line_numb, \$p file1 > file2

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!
Sponsored Links
    #6  
Old Unix and Linux 12-15-2004
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 27 April 2015, 10:26 AM EDT
Location: Ashburn, Virginia
Posts: 9,931
Thanks: 64
Thanked 449 Times in 264 Posts
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.
Sponsored Links
    #7  
Old Unix and Linux 12-15-2004
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 27 April 2015, 10:26 AM EDT
Location: Ashburn, Virginia
Posts: 9,931
Thanks: 64
Thanked 449 Times in 264 Posts
Re: sed -n $line_numb, \$p file1 > file2

Quote:
Originally posted by whatisthis
Perderabo,
What does \\c mean here?

echo bogus\\c >> textfile
It mean to suppress the newline.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -4. The time now is 03:55 PM.