I'm reading sed & awk
book and I thought that I understood the n
command of sed
till I executed the following:
$ echo -ne "abc\ncde\nfg\n" | sed "/c/{
n
/f/d
}"
Output:
abc
cde
fg
But I expected the line fg
to be deleted.
My understanding of the process:
c
matches the linecde
, the next line isfg
and should be deleted by the/f/d
command as it is matched byf
.
I was so sure that I understand this command and it is simple. Especially because the authors write that the uppercase commands N,D,P
commands are more difficult and if you understand them then you understand the lowercase commands anyway. But I have no difficulties with the N
command at all.
sed '/c/{N;P;/f/!{D};d}'
. – 123 Jul 22 '16 at 09:18n
doesn't return to the top of the script (i.e. it doesn't restart the command cycle), it just runs the remaining commands (so the conditional block/c/{..}
is never applied to thecde
line); and just for fun: edit the last line to readcfg
and you'll see it won't be deleted, even if it does match/c/
and/f/
– don_crissti Jul 22 '16 at 09:21f
in it that follows an odd number of records with ac
in it. – ctrl-alt-delor Jul 22 '16 at 09:33