Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-02-2012
Registered User
 
Join Date: Jul 2009
Posts: 69
Thanks: 24
Thanked 1 Time in 1 Post
sed "-n" switch

Hi Guys,

i'm exploring sed and failed to understand the following. Can anyone with more knowledge of this explain this better.

I have to read lines 4 to 6 in a file so i used the following command :


Code:
sed '4,6 p' file

but the above prints all lines instead! . when i use the -n switch however it prints the desired lines only.

I know that the -n switch prevents sed from printing to console but how exactly is it affecting my above example. Please advise.

Thanks
Sponsored Links
    #2  
Old 07-02-2012
Registered User
 
Join Date: Oct 2007
Location: USA
Posts: 1,303
Thanks: 11
Thanked 99 Times in 95 Posts
From the sed man page...

Code:
-n    Suppress the default output (in which each line, after it is examined for editing, is written to standard output). Only lines explicitly selected for output are written.

The Following User Says Thank You to shamrock For This Useful Post:
Irishboy24 (07-02-2012)
Sponsored Links
    #3  
Old 07-02-2012
Registered User
 
Join Date: Jul 2009
Posts: 69
Thanks: 24
Thanked 1 Time in 1 Post
thanks guys. this explains it..i'm using cygwin on my home laptop and the man page on there was not that explicit.

Cheers
    #4  
Old 07-02-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,372
Thanks: 87
Thanked 478 Times in 458 Posts
A little more explanation....

sed works line by line. Each line in the input is read into a pattern space and then all the edits in your script are attempted on that pattern space. At the end of the script, the pattern space is output, irrespective of whether any or all or none of the edits affected the pattern space. This is the default behaviour of sed. That is precisely why it's called a stream editor/filter. It works on a stream; what goes in, comes out, albeit changed in some cases.

The -n switch (or the #n comment line in scripts) suppresses this default behaviour. In such cases, sed will output the pattern space only when it is told to do so explicitly.

In your command (without the -n switch), lines 1-3 and those after line 6 will just pass through the script unaffected. Lines 4-6 will match the address of the print command and will be output:
1) by your print command, and then
2) by sed's default behaviour.

Last edited by elixir_sinari; 07-02-2012 at 02:06 PM..
The Following User Says Thank You to elixir_sinari For This Useful Post:
Irishboy24 (07-02-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 08:59 AM
Using the "Less Than" (-lt) switch on numbers with decimal places? Glyn_Mo Shell Programming and Scripting 7 05-20-2009 07:34 AM
find and "-print" switch MartyIX Shell Programming and Scripting 13 09-04-2008 07:26 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 12:52 AM



All times are GMT -4. The time now is 09:49 AM.