|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|