without preamble,
$ cat in.txt a b c d $ sed \'=;N\' in.txt 1 a b 3 c d
It looks like \'N\' command works on every other line. Maybe that wou
Excerpt from info sed
:
`sed' operates by performing the following cycle on each lines of
input: first, `sed' reads one line from the input stream, removes any
trailing newline, and places it in the pattern space. Then commands
are executed; each command can have an address associated to it:
addresses are a kind of condition code, and a command is only executed
if the condition is verified before the command is to be executed.
...
When the end of the script is reached, unless the `-n' option is in
use, the contents of pattern space are printed out to the output
stream,
...
Unless special commands (like 'D') are used, the pattern space is
deleted between two cycles
...
`N'
Add a newline to the pattern space, then append the next line of
input to the pattern space. If there is no more input then `sed'
exits without processing any more commands.
...
`D'
Delete text in the pattern space up to the first newline. If any
text is left, restart cycle with the resultant pattern space
(without reading a new line of input), otherwise start a normal
new cycle.
This should pretty much resolve your query. But still I will try to explain your three different cases:
CASE 1:
sed
reads a line from input. [Now there is 1 line in pattern space.]=
Prints the current line no.N
reads the next line into pattern space.[Now there are 2 lines in pattern space.]
sed
prints the pattern space and cleans it. [Pattern space is empty.]EOF
reached sed
exits here. Else Restart the complete cycle from step 1. [ie: In case of even lines, sed exits here.]Summary: In this case sed reads 2 lines and prints 2 lines at a time. Last line is swallowed it there are odd lines (see step 3).
CASE 2:
sed
reads a line from input. [Now there is 1 line in pattern space.]N
reads the next line into pattern space. [Now there are 2 lines in pattern space.]
$!
) print the first line(P
) from pattern space. [The first line from pattern space is printed. But still there are 2 lines in pattern space.]$!
) delete the first line(D
) from pattern space [Now there is only 1 line (the second one) in the pattern space.] and restart the command cycle from step 2. And its because of the command D
(see the excerpt above).$
) then delete(d
) the complete pattern space. [ie. reached EOF ] [Before beginning this step there were 2 lines in the pattern space which are now cleaned up by d
- at the end of this step, the pattern space is empty.]sed
automatically stops at EOF.Summary: In this case :
sed
reads 2 lines first.CASE 3:
Its the same case as CASE:1, just remove the Step 2 from it.