Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 1
    @Geek No, the h command copies the pattern space to the hold space, which persist until sed ends. After the end of the script everything is cleared, because the binary exited. Commented Sep 30, 2015 at 14:38
  • 2
    Can we think of the hold space like registers for vim? Are they also numbered? Or there is only one of them? Commented Sep 30, 2015 at 14:41
  • 2
    @Geek In sed there is only one hold space. It's like a variable which can contain something. Commented Sep 30, 2015 at 14:44
  • 1
    @user1717828 If we wouldn't the first line would be printed, when processed. Since sed is not invoked with -n we have to delete every line except the last. At the last line sed appends everything from the hold space to the pattern space. And because the d command will not be executed, the line is printed (this line contains now the whole file reversed). Commented Sep 30, 2015 at 19:54
  • 2
    (1) The OP’s sub-question/observation (in the comment) is, “so the h command on the last line is sort-of a no-op.” (with added emphasis, and slightly paraphrased).  He’s right; when sed is processing the last line of input, the G reads the hold space (in order to append it to the pattern space), and then the h copies the pattern space to the hold space, which is never referenced again.  We could just as well say sed 'G;$!h;$!d' or sed 'G;$!{h;d}'.  (2) We could avoid using d by saying sed -n 'G;h;$p'. Commented Oct 1, 2015 at 7:14