Issue
The sed manual states about the N command:
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.
Now, from what I know, sed reads each line of input, applies the script(s) on it, and (if -n is not specified) prints it as the output stream.
So, given the following example file:
Apple
Banana
Grapes
Melon
Running this:
$ sed '/Banana/N;p'
From what I understand, sed should process each line of input: Apple, Banana, Grapes and Melon. So, I would think that the output will be:
Apple
Apple
Banana
Grapes # since it read the next line with N
Banana
Grapes
Grapes (!)
Grapes (!)
Melon
Melon
Explanation:
Apple
is read to the pattern space. it doesn't match Banana
regex, so only p
is applied. It's printed twice: once for the p
command, and once because sed prints the pattern space by default.
Next, Banana
is read to the pattern space. It matches the regex, so that the N command is applied: so it reads the next line Grapes
to the pattern space, and then p
prints it: Banana\nGrapes
. Next, the pattern space is printed again due to the default behavior.
Now, I would expect that Grapes
will be read to the pattern space, so that Grapes
will be printed twice, same as for Apple and Melon.
But in reality, this is what I get:
Apple
Apple
Banana
Grapes
Banana
Grapes
Melon
Melon
It seems that once Grapes
was read as part of the N command that was applied to Banana
, it will no longer be read as a line of its own.
Is that so? and if so, why isn't it emphasized in the docs?
Solution
This might explain it (GNU sed):
sed '/Banana/N;p' file --d
SED PROGRAM:
/Banana/ N
p
INPUT: 'file' line 1
PATTERN: Apple
COMMAND: /Banana/ N
COMMAND: p
Apple
END-OF-CYCLE:
Apple
INPUT: 'file' line 2
PATTERN: Banana
COMMAND: /Banana/ N
PATTERN: Banana\nGrapes
COMMAND: p
Banana
Grapes
END-OF-CYCLE:
Banana
Grapes
INPUT: 'file' line 4
PATTERN: Melon
COMMAND: /Banana/ N
COMMAND: p
Melon
END-OF-CYCLE:
Melon
Where --d
is short for --debug
You will see the INPUT:
lines go 1,2,4
because the second cycle also grabs input line 3 with the N
command.
Answered By - potong Answer Checked By - Senaida (WPSolving Volunteer)