removing end of line mark with sed

WATANABE Katsuhiro katsu at sra.co.jp
Mon Aug 27 18:29:51 AEST 1990


# Sorry for my poor English.

In article <602 at risky.Convergent.COM> chrisb at risky.Convergent.COM (Chris Bertin) writes:

> > | How do I remove the end of line marker from a stream?  for example,

> (cat $1; echo '\n_E_o_F_\n') | \
>    sed -e ':top' -e '1,/_E_o_F_/ N' -e 's/\n/ /' -e 't top' -e 's/ _E_o_F_//'
> 
> Note that:
>         sed -e ':top' -e '1,$ N' -e 's/\n/ /' -e 't top'
> doesn't work because sed won't print lines that are not terminated by a
> carriage return (bug?).

  Wrong.

  It is probable that unix standard sed doesn't take a (the last) input
line that isn't terminated by a CR into pattern space. But, it isn't 
related to this question. The trouble of your second script is caused by
`1,$ N'. `N' at $(last input line) will terminate sed processing
immediately without printing pattern space. I think it is natural
because sed neither can get any more input line nor can determine
current line number after `N'. Do you approve of the line number `$+1' ? :-) 

(Gnu sed continues processing after `N' at $.
QUIZ: How does it turn out?
        echo '' | GNUsed -e 'N' -e '='
      And how?
        echo '' | GNUsed -e 'N' -e '=' -e 'N' -e '='      
)


> If sed allowed addresses like '$-1', it would
> make life simpler as well.

  Here is another way without _E_o_F_ (without `$-1', also)

        sed -e ':top' -e '$q' -e 'N' -e 's/\n/ /' -e 'b top'



  As you all know, using `tr' is more easy and more reliable. There
are some risks of buffer over flow in using `N' of unix standard sed.
(Gnu sed will work fine, because it allocates pattern space dynamically.)
For instance, you will miss some byte stuff if you apply above example to
/bin/sh. (In case of /bin/sed on my Sony NEWS, it crashes and dumps core.)

----____----____
WATANABE Katsuhiro      Software Research Associates, Inc. Japan.
Not execute, but evaluate.



More information about the Comp.unix.questions mailing list