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 :
- line 1 is read as input in pattern space (1)
- then the 'n' command write the pattern space in stdout (1 is printed) since the default output as not been suppressed with the '-n' option
- the 'p' command print the entire pattern space (1 is printed again)
- default output is printed (1 is printed once again)
second cycle :
- pattern space is cleared
- line 2 is read as input in pattern space (2)
- then the 'n' command write the pattern space in stdout (2 is printed)
- the 'p' command prints the entire pattern space in stdout (2 is printed again)
- default output is printed (2 is printed once again)
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 ?