2

The document of sed states that d will:

Delete the pattern space; immediately start next cycle.

If sed is run without -n, will sed auto print the pattern space after a d command? I know at this situation, the pattern space is empty. So it's no differences to print or not. But I'm just curious.

I have this example:

root@u1804:~# seq 10 | sed -r '/5/d'
1
2
3
4
6
7
8
9
10
root@u1804:~#

As you can see, there is no 5. It's because the d caused the execution directly to the next cycle or the auto print pattern space prints an empty string?

2

Yes, d restarts the cycle so no auto-printing. It's easy to see that if you attempt to append to the pattern space after a d command e.g via G or s/^/STUFF/ nothing gets printed...
Try seq 3 | sed '1h;2d;2G' - it will not print 1 twice even though you save it via 1h and then try to append it via 2G for the simple reason the last command is never executed - everything that follows after d (including auto-printing) is ignored (for the current address, that is).
The standard is pretty clear:

If no commands explicitly started a new cycle, then at the end of the script the pattern space shall be copied to standard output (except when -n is specified) and the pattern space shall be deleted.

It's also in the manual that you quoted:

By default sed prints all processed input (except input that has been modified/deleted by commands such as d)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.