What does `-n` option in sed do?
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 requestedsed '2,3p' test.txt
- prints each line (automatically), AND ALSO prints lines 2-3 a second timesed -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 firstp
command then the auto print), and line 3 three times (once for eachp
command, then again automatically).
Related videos on Youtube
dotancohen
Updated on September 18, 2022Comments
-
dotancohen over 1 year
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 over 9 yearsThank 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. -
Gordon Davisson over 9 yearsNo, in both cases
-n
is doing the exact same thing: switching from every line being automatically printed (and thep
andp
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 thep
command etc). -
Gordon Davisson over 9 yearsPut 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. -
dotancohen over 9 yearsI see now, thank you. I very much appreciate your taking the time to help make the example explicit. Have a terrific week!
-
Timo almost 4 years@GordonDavisson what is the difference between
,
and;
? -
Gordon Davisson almost 4 years@Timo In this case,
;
separates commands (so in2,3p; p
,2,3p
is the first command, andp
is a second, separate, command), and,
separates the starting and ending addresses (so2,3p
means apply thep
command starting at line 2 and ending at line 3). Of course, both symbols can have other meanings in other contexts. See thesed
man page for more details.