You're viewing a comment by Shiron and its responses.

Shiron Permalink
February 18, 2014, 08:37

There's something I don't understand about sed multiple commands. Take example 49:

sed -e '$!{h;d;}' -e x
which could also be written as:
sed '$!{h;d;};x'

In both examples, why isn't the x executed for all lines?

for the first set of commands we say: "do this for all lines except for the last line" but the second command "x" isn't restricted to any line. It should run for every line, therefore exchanging the content of the pattern/hold buffers and printing, resulting in EVERY line except the last to be printed. Instead it's only executed for the last line.
The "-e" multiple command option or the ";" (semicolon which as far as I can tell acts the same as "-e") should work for every line unless restricted by a range operator which in this case it's not, yet it still works. WHY???

Comment Responses

Chris Permalink
May 19, 2014, 06:41

Because the 'd' command automatically begins a new cycle with the next line after clearing the pattern space.

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "coding_117": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.