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: Michael Green
Subject: Re: Why isn't "sed -n p" identical to "cat"?
Date: Wed, 9 Jan 2019 11:12:29 +0100
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.3.3

Hello,

thank you for your help.

I failed to consider multiple input files at all. I thought multiple files are considered one input stream, this behavior would be easier to understand for me if the -s option was used. Which brings me to my question if something in the mentioned behavior differs when the -s option is used?

I would also like to ask two additional questions:

1) Is sed '' identical to sed -n p ?

2) The second example is logical and was very helpful. However, it raises a question of where the newline is coming from in the following command: printf a | sed -n 'x;p'. The hold buffer should be empty, after printing, sed doesn't output more to the same output stream so my understanding is that no newline should be added by sed

Thank you very much for your time

Michal G.

On 1/8/2019 10:59 PM, Assaf Gordon wrote:
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]