I am trying to understand how the 'n' command of sed operates.
I am on Mac OS X, so it is sed from BSD
Let's consider this example :
$ seq 5 | sed -e 'n;p'
1
2
2
3
4
4
5
According to the Apple manpage here (see the quote below) and my understanding, the output should rather be this :
1
1
1
2
2
2
3
3
3
4
4
4
5
5
5
Apple manpage about sed :
[2addr]n
Write the pattern space to the standard output if the default output has not been suppressed, and replace the pattern space with the next line of input.
How I expect sed to manage this :
first cycle :
second cycle :
etc.
I also found this tutorial on (GNU ?) sed, but I didn't help me either to understand how this command line works (what does the '*' mean in the "Modification to Output Stream" column ?)
Can somebody explain to me what is going on step by step please ? Why some numbers are printed only once ? Is default output replace by the 'n' first function that is to write pattern space in stdout ? is pattern space really modified by 'n' or not ?
Thank you for the reference to my tutorial on sed, which was written for the original AT&T/Sun sed. I do my best to keep it up to date with the various extensions and variations, e. g. GNU sed.
Don't forget, the 'n' command reads in the next line into the pattern space, which "flushes" the existing pattern space. The 'N" command doesn't "flush", but appends the pattern space.
Here is what happens with the 'n;p' commands
Sed now repeats the above steps again. Which reads in "3", prints it, then reads in "4" and prints it twice, etc.
If, for instance, you used
seq 10 | sed -n -e 'n;p'
then sed would print every other line. That is,
Does this help?
And when I mention "modification to the output", it depends if the '-n' command line option is used or not. See the bottom of that chart.