Does deleting a template with space "d" erase hold?

Can someone explain why this is happening?

Expected:

$ echo -e "foo\nbar" | sed -n 'h; x; p' foo bar 

I put each line in the hold space, then I change the place to hold and the picture space, and then print the pattern space so that each line is printed. Now, why are the following different?

 $ echo -e "foo\nbar" | sed -n 'h; d; x; p' 

I thought that this would not happen, because I am deleting the template space before replacing it, so the saved string should be returned back to the template space. This is the hold space that should be empty after x; , right? I delete the template space,, then . Where is the line that I saved?

+4
source share
2 answers

I assume this is due to the following line in man man sed :

d Delete pattern space. Start the next cycle.

The following works as expected:

 $ echo -e "foo\nbar" | sed -n 'h; s/.*//; g; p' foo bar 

Sorry to bother you guys.

+3
source

When you use d , the template space is cleared, the next line is read, and processing starts at the beginning of the script. This way you never reach steps x and p , instead, just copy to hold space and delete.

+5
source

Source: https://habr.com/ru/post/1412734/


All Articles