sed -n | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

sed -n

Shell Programming and Scripting


Tags
linux

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-15-2004
whatisthis's Avatar
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
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 12-15-2004
zazzybob's Avatar
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 24 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 12-15-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 24 October 2014, 3:06 PM EDT
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 437 Times in 261 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!
    #4  
Old 12-15-2004
zazzybob's Avatar
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 24 Times in 23 Posts
Well; without the space between , and \ it is legal! 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 12-15-2004
whatisthis's Avatar
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 12-15-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 24 October 2014, 3:06 PM EDT
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 437 Times in 261 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 12-15-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 24 October 2014, 3:06 PM EDT
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 437 Times in 261 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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -4. The time now is 07:33 PM.