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:
cmatches the linecde, the next line isfgand should be deleted by the/f/dcommand 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:18ndoesn'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 thecdeline); and just for fun: edit the last line to readcfgand you'll see it won't be deleted, even if it does match/c/and/f/– don_crissti Jul 22 '16 at 09:21fin it that follows an odd number of records with acin it. – ctrl-alt-delor Jul 22 '16 at 09:33