sed-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why isn't "sed -n p" identical to "cat"?


From: Assaf Gordon
Subject: Re: Why isn't "sed -n p" identical to "cat"?
Date: Tue, 8 Jan 2019 14:59:30 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0

Hello,

On 2019-01-08 9:24 a.m., Michael Green wrote:
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?

yes. but see below...

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"?

The key in Footnote 8 (which you quoted) is:

"as soon as more text is sent to the same output stream"
How can "more text" be sent to the output stream after the "last line"?

It might not be intuitive, but sed (and cat) can process multiple files,
and so the "last line" in a file is not the last line the input stream.

Consider the following:

  $ printf A > a
  $ printf B > b
  $ printf C > c

  $ cat a b c | od -tc -An
     A   B   C

  $ sed -n p a b c | od -tc -An
     A  \n   B  \n   C

cat(1) prints the input exactly as-is, no modifications.
It literally concatenates multiple input files into one output stream
(hence the name 'cat', from 'concat'). BTW for this reason you can also
concatenate binary files with cat(1).

'sed -n p' sees that a newline is missing between the last line
of the input (files 'a', 'b') and the next file, and inserts a  newline.


Another example (a bit contrived):

  $ printf "a\nb" | sed -n p | od -tc -An
     a  \n   b

  $ printf "a\nb" | sed -n 'p ; $p' | od -tc -An
     a  \n   b  \n   b

In the second invocation, the last line is printed twice.
The input line "b" did not contain a newline, but the output
does contain a newline - sed injected it once it realized
more output is coming after a line that does not have a newline.


Hope this answers your question.
regards,
 - assaf





reply via email to

[Prev in Thread] Current Thread [Next in Thread]