[comp.unix.questions] removing end of line mark with sed

spam@hobbes.cc.iastate.edu (Begley Michael L) (08/20/90)

Here's a simple one...
How do I remove the end of line marker from a stream?  for example,
if I have a stream that esentially looks like:
1
2
3
how do I make it into:
1   2   3

I tried      sed 's/\n//'      but that didn't work.  Thanks.

-mike begley
spam@hobbes.cc.iastate.edu
begley@forest.ecil.iastate.edu

merlyn@iwarp.intel.com (Randal Schwartz) (08/20/90)

In article <1990Aug19.194911.16628@fs-1.iastate.edu>, spam@hobbes (Begley Michael L) writes:
| Here's a simple one...
| How do I remove the end of line marker from a stream?  for example,
| if I have a stream that esentially looks like:
| 1
| 2
| 3
| how do I make it into:
| 1   2   3
| 
| I tried      sed 's/\n//'      but that didn't work.  Thanks.

I horsed around with the 'N' command in sed, thinking I could make it
do that, and figured out that I had lost the sed touch.  (Somebody
will most certainly post the proper way to do it.)  Try

tr '\012' ' '

instead of sed.  If you really need the trailing newline, and you have
a shell with a builtin echo, try:

...some_command |
some_command_that_produces_those_lines |
while read x
do echo -n "$x "
done
echo ""

or

echo `some_command_that_produces_those_lines`

depending on how you get your data.  The first one is *really*
expensive if you don't have a builtin echo, but the second one isn't
bad.

Obligatory Perl reference:

perl -pe 's/\n/ / unless eof;'

Just another (former) sed user,
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

hamlin@blackbird.afit.af.mil (Joe Hamlin) (08/20/90)

merlyn@iwarp.intel.com (Randal Schwartz) writes:

>In article <1990Aug19.194911.16628@fs-1.iastate.edu>, spam@hobbes (Begley Michael L) writes:
>| [How to turn]:
>| 1
>| 2
>| 3
>| into:
>| 1   2   3

>I horsed around with the 'N' command in sed, thinking I could make it
>do that, and figured out that I had lost the sed touch.  (Somebody
>will most certainly post the proper way to do it.)  Try

Don't know about "proper", but this seems to work:

sed -n -e '1{;h;$p;b' -e '}' -e 'H;$x;$s/\n/ /gp'

Seems a bit ugly, though.  It's not absolutely clear from the
problem, how many spaces/tabs are wanted as separator(s), or
whether or not a trailing newline is required.  Add spaces to
taste.

>tr '\012' ' '

>instead of sed.  If you really need the trailing newline, and you have
>a shell with a builtin echo, try:

>[Randal's echo script]

or:
(tr '\012' ' '; echo)

or:
>echo `some_command_that_produces_those_lines`

Both the tr and single line echo solutions are limited to a single
space separator.

>perl -pe 's/\n/ / unless eof;'
It's even readable. :-)

And in awk:

awk '{printf "%s ", $0} END {print}'
-- 
Joe Hamlin    <hamlin@blackbird.afit.af.mil>

ARaman@massey.ac.nz (A.V. Raman) (08/20/90)

In article <1990Aug19.194911.16628@fs-1.iastate.edu> spam@iastate.edu (Begley Michael L) writes:
>Here's a simple one...
>How do I remove the end of line marker from a stream?  for example,
>
>I tried      sed 's/\n//'      but that didn't work.  Thanks.

tr -d \\012

will do the trick.

-& (Anand)

jetzer@studsys.mu.edu (Mike Jetzer) (08/21/90)

In article <1990Aug19.194911.16628@fs-1.iastate.edu> spam@iastate.edu (Begley Michael L) writes:
>How do I remove the end of line marker from a stream?  for example,
>if I have a stream that esentially looks like:
>1
>2
>3
>how do I make it into:
>1   2   3

Try tr(1):

tr \\012 " " < file

Will replace all newlines (linefeeds, octal 012) with a space.  Replacing
linefeeds with tabs or some other character is left as an exercise.

If you want more than one space (as your example seems to show), you
can run the output of tr through sed or awk or whatever.

-- 
Mike Jetzer
"And we'll have fun, fun, fun until Daddy takes the keyboard awa-ay..."

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (08/21/90)

In article <768@studsys.mu.edu> jetzer@studsys.UUCP (Mike Jetzer) writes:
  [ trimmed ]
> In article <1990Aug19.194911.16628@fs-1.iastate.edu> spam@iastate.edu (Begley Michael L) writes:
> >How do I remove the end of line marker from a stream?
> tr \\012 " " < file
> If you want more than one space (as your example seems to show), you
> can run the output of tr through sed or awk or whatever.

This is a bad idea, as spaces in the original would also be expanded. Do
the sed/awk bit first.

I'm about to start coding str, a ``string translator,'' to do everything
tr can do but with more than one character as a unit. It's not as if
other tools don't do the job; but somehow the syntax never seems right.

---Dan

gt0178a@prism.gatech.EDU (BURNS,JIM) (08/23/90)

in article <1990Aug19.194911.16628@fs-1.iastate.edu>, spam@hobbes.cc.iastate.edu (Begley Michael L) says:

> How do I remove the end of line marker from a stream?  for example,
> how do I make it into:
> 1   2   3

> I tried      sed 's/\n//'      but that didn't work.  Thanks.


It's not sed, but it's kind of cute:

x=`cat filename`
echo $x|cat >filename2
echo ''>>filename2
-- 
BURNS,JIM
Georgia Institute of Technology, Box 30178, Atlanta Georgia, 30332
uucp:	  ...!{decvax,hplabs,ncar,purdue,rutgers}!gatech!prism!gt0178a
Internet: gt0178a@prism.gatech.edu

chrisb@risky.Convergent.COM (Chris Bertin) (08/25/90)

In article <1990Aug19.234327.19785@iwarp.intel.com>, merlyn@iwarp.intel.com (Randal Schwartz) writes:
> In article <1990Aug19.194911.16628@fs-1.iastate.edu>, spam@hobbes (Begley Michael L) writes:
> | Here's a simple one...
> | How do I remove the end of line marker from a stream?  for example,
> 
> I horsed around with the 'N' command in sed, thinking I could make it
> do that, and figured out that I had lost the sed touch.  (Somebody
> will most certainly post the proper way to do it.)  Try
> 
> /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\

Here is an ugly way to do it with sed:

(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?).  If sed allowed addresses like '$-1', it would
make life simpler as well.
-- 
Chris Bertin		|   chrisb@risky.Convergent.COM
Unisys			|		or
(408) 435-3762		| ...!uunet!pyramid!ctnews!risky!chrisb

katsu@sra.co.jp (WATANABE Katsuhiro) (08/27/90)

# Sorry for my poor English.

In article <602@risky.Convergent.COM> chrisb@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.