Re: Help in understanding sed's behavior at end of cycle

"Daniel Goldman [email protected] [sed-users]" <[email protected]> Sun, 13 Oct 2019 17:23:43 -0700
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
> Compare:
>  A) echo -n a|sed 's:$:b:'
>  B) echo -n a|sed 's:$:b:;G'
>
> To me the output of A) is correct, but that of B) is wrong,
> since it has two trailing newlines instead of one.

It is a reasonable question to ask.

First, as to what happens at the end of a cycle, the original question, 
you have it correct: "sed adds the current line into pattern space (by 
dropping the newline if any), runs the code and prints the pattern space 
(adding the dropped newline if any was there initially)".

gnu sed manual says: "first, sed reads one line from the input stream, 
removes any trailing newline, and places it in the pattern space. ... 
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."

To me, it is the same logic, with the addition of -n explanation.

//////////////

I use sed a lot, and for some reason never use input that lacks a 
trailing newline. So I understand basically agree with Jim's and answer. 
As to whether a line without a trailing newline is "malformed" or an 
"error", I'm not sure I agree with that. Maybe a matter of semantics. 
sed certainly has to take the case into account, even if not the usual 
input, even if not input I would ever use.

//////////////

Second, as to why B) has two trailing newlines, GNU sed manual says: 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” ...

So, the answer seems "least expected surprise". More text followed. So 
sed printed a newline after the line without a newline. sed does not 
make the special case "Well, the next line is only a newline, so I won't 
add that newline after the line that was not supposed to have a 
newline". Instead, it says "Well, there is more text of some fashion, so 
I'll add a newline after that line without a newline."

Look at the following example:

$ echo -n abc | sed "a xyz"

The result that is (probably) more consistent with sed's line-oriented 
nature, the result that is actually produced:

abc
xyz

What happens if newline NOT added to line without a newline:

abcxyz

I think it is a good question you asked. I suppose it is a somewhat 
arbitrary decision how to handle the special case where no newline and 
more text follows. Personally, I think the way it is handled is fine. 
Does anyone have an example where it causes a problem?

Daniel


------------------------------------

------------------------------------

-- 

------------------------------------

Yahoo Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/sed-users/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/sed-users/join
    (Yahoo! ID required)

<*> To change settings via email:
    [email protected] 
    [email protected]

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo Groups is subject to:
    https://info.yahoo.com/legal/us/yahoo/utos/terms/