|
From: | Michael Green |
Subject: | Why isn't "sed -n p" identical to "cat"? |
Date: | Tue, 8 Jan 2019 17:24:56 +0100 |
User-agent: | Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.3.3 |
Hello,I am a little confused by section 6.1 in the manual. Especially the footnote #FOOT8. The manual states:
Section 6.1: When the end of the script is reached, unless the "-n" option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed[8].
Footnote 8: Actually, if "sed" prints a line without the terminating newline, it will nevertheless print the missing newline as soon as more text is sent to the same output stream, which gives the “least expected surprise” even though it does not make commands like "sed -n p" exactly identical to "cat".
I tried executing "printf 'aaa\nbbb\nccc' | sed -n p" which correctly printed "ccc" with no trailing newlines. If there is a line that does not have any newlines to remove, it must be the last line, am I correct? How can "sed" send more text to the same stream if it doesn't have any more lines to process? Could you please give me an example demonstrating this behavior and the difference between "sed -n p" and "cat"?
Thank you for your time, Michael G.
[Prev in Thread] | Current Thread | [Next in Thread] |