26

This is the man page entry for -n:

-n

suppress automatic printing of pattern space

I notice that when not using -n for certain operations, each line is printed to stdout (and the requested lines are printed twice):

$ cat test.txt 
first
second
third
fourth
fifth

$ sed -n '2,3p' test.txt 
second
third

$ sed '2,3p' test.txt 
first
second
second
third
third
fourth
fifth

However, this law does not hold for other commands:

$ sed -n 's/t/T/' test.txt 

$ sed 's/t/T/' test.txt 
firsT
second
Third
fourTh
fifTh

So what does -n do, exactly?

dotancohen
  • 11,278
  • 19
  • 67
  • 96

1 Answers1

30

Normally, sed processes each line (doing substitutions etc), then prints the result. If the processing involves the line being printed (e.g. sed's p command), then it gets printed twice (once during processing, then again by the automatic post-processing print). 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.

  • sed -n '2,3p' test.txt - prints only lines 2 through 3, as requested

  • sed '2,3p' test.txt - prints each line (automatically), AND ALSO prints lines 2-3 a second time

  • sed -n 's/t/T/' test.txt - replaces "t" with "T" on each line, but doesn't print the result due to -n

  • sed 's/t/T/' test.txt - replaces "t" with "T" on each line, and automatically prints the result

And let me add some more examples:

  • sed -n 's/t/T/p' test.txt - replaces "t" with "T" on each line, prints ONLY lines where the substitution took place (i.e. not "second")

  • sed 's/t/T/p' test.txt - replaces "t" with "T" on each line, prints lines where the substitution took place, then automatically prints each line (result: "second" is printed once, all the others twice)

  • sed '2,3p; 3p' test.txt - prints lines 1, 4, and 5 once (the auto print); line 2 twice (the first p command then the auto print), and line 3 three times (once for each p command, then again automatically).

Gordon Davisson
  • 34,084
  • 5
  • 66
  • 70
  • Thank you Gordon but your assessment only repeats my observation. In the first two examples sed without `-n` prints out every line in addition to those lines specifically requested. Thus in that case `-n` **suppresses the printing of every line parsed**. However in the third and fourth examples the behaviour is different. In those cases sed without `-n` does not output the lines twice, thus using `-n` does not _suppress the printing of every line parsed_ but rather **suppresses the printing of lines not changed**. I am having difficulty generalizing when the behaviour will be one or the other. – dotancohen Dec 14 '14 at 19:48
  • No, in both cases `-n` is doing the exact same thing: switching from every line being automatically printed (and the `p` and `p` modifier to a substitute command cause the line to be printed additional times) to a mode where lines are printed *only* if you specifically tell it to (with the `p` command etc). – Gordon Davisson Dec 14 '14 at 20:40
  • Put it yet another way: try adding the `-n` option AND ALSO adding `; p` to the sed command (e.g. `sed -n '2,3p; p'`, and you'll get the same results as you would have without either. The `-n` suppresses the default automatic print, and the `; p` adds an explicit (mostly equivalent) print. – Gordon Davisson Dec 14 '14 at 20:42
  • I see now, thank you. I very much appreciate your taking the time to help make the example explicit. Have a terrific week! – dotancohen Dec 14 '14 at 20:49
  • @GordonDavisson what is the difference between `,` and `;`? – Timo Aug 17 '20 at 19:12
  • @Timo In this case, `;` separates commands (so in `2,3p; p`, `2,3p` is the first command, and `p` is a second, separate, command), and `,` separates the starting and ending addresses (so `2,3p` means apply the `p` command starting at line 2 and ending at line 3). Of course, both symbols can have other meanings in other contexts. See [the `sed` man page](https://linux.die.net/man/1/sed) for more details. – Gordon Davisson Aug 17 '20 at 19:15