Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What actually the meaning of "-n" in sed?

According to http://linux.about.com/od/commands/l/blcmdl1_sed.htm

suppress automatic printing of pattern space

I've tested with or without -n, sed will produce same result

I dont understand what space does it means.

like image 600
Jon Maikel Avatar asked May 04 '11 16:05

Jon Maikel


People also ask

What does N mean in sed?

N reads the next line into pattern space. [Now there are 2 lines in pattern space.] If there is no next line to read then sed exits here. [ie: In case of odd lines, sed exits here - and hence the last line is swallowed without printing.]

What is '- n option in Linux?

The -n option disables the automatic printing, which means the lines you don't specifically tell it to print do not get printed, and lines you do explicitly tell it to print (e.g. with p ) get printed only once.

What is S in sed?

Substitution command In some versions of sed, the expression must be preceded by -e to indicate that an expression follows. The s stands for substitute, while the g stands for global, which means that all matching occurrences in the line would be replaced.

What is D in sed?

The d command is used to delete lines. After sed copies a line from a file and puts it into a pattern buffer, it processes commands on that line, and, finally, displays the contents of the pattern buffer on screen.


2 Answers

Sed has two places to store text: pattern space and hold space. Pattern space is where each line is put to be processed by sed commands; hold space is an auxiliary place to put some text you may want to use later. You probably will use only pattern space.

Before sed goes to process a line, it is put in the pattern space. Then, sed applies all commands (such as s///) to de pattern space and, by default, prints the resulted text from the pattern space. Let us suppose we have a file myfile with a line like:

The quick brown fox jumps over the lazy dog.

We run the following command:

sed 's/fox/coati/;s/dog/dingo/' myfile

Sed will apply s/fox/coati/ and then s/dog/dingo/ for each line of the file - in this case, the only one we showed above. When it occurs, it will put the line in the pattern space, which will have the following content:

The quick brown fox jumps over the lazy dog.

Then, sed will run the first command. After sed runs the command s/fox/coati/, the content of the pattern space will be:

The quick brown coati jumps over the lazy dog.

Then sed will apply the second command, which is s/dog/dingo/. After that, the content of the pattern space will be:

The quick brown coati jumps over the lazy dingo.

Note that this only happens in memory - nothing is printed by now.

After all commands have been applied to the current line, by default, sed will then get the content of the pattern space and print it to the standard output. However, when you give -n as an option to sed, you are asking sed to do not execute this last step except if it is explicit required. So, if you run

sed -n 's/fox/coati/;s/dog/dingo/' myfile

nothing will be printed.

But how could you explicitly request sed to print the pattern space? Well, you can use the p command. When sed finds this command, it will print the content of the pattern space immediately. For example, in the command below we request sed to print the content of the pattern space just after the first command:

sed -n 's/fox/coat/;p;s/dog/dingo/' myfile

The result will be

$ sed -n 's/fox/coati/;p;s/dog/dingo/' myfile 
The quick brown coati jumps over the lazy dog.

Note that only fox is replaced. It happens because the second command were not executed before the pattern space being printed. If we want to print the pattern space after both commands, we just put p after the second one:

sed -n 's/fox/coati/;s/dog/dingo/;p' myfile

Another option, if you are using the s/// command, is to pass the p flag to s///:

sed -n 's/fox/coati/;s/dog/dingo/p' myfile 

In this case, the line will only be printed if the flagged replacement was executed. It may be very useful!

like image 96
brandizzi Avatar answered Nov 14 '22 02:11

brandizzi


Just try a sed do-nothing:

sed '' file

and

sed -n '' file

First will print whole file but second will NOT print anything.

like image 20
anubhava Avatar answered Nov 14 '22 02:11

anubhava