[comp.unix.questions] Deleting newlines

barajas@manta.NOSC.MIL (Albert A. Barajas) (01/05/90)

	hi,
		Is there a quick and dirty way to delete newlines
		from an ascii text file?

		thanks,

		albert
		barajas@manta.nosc.mil

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (01/05/90)

In article <990@manta.NOSC.MIL> barajas@manta.NOSC.MIL (Albert A. Barajas) writes:
: 		Is there a quick and dirty way to delete newlines
: 		from an ascii text file?

Lotsa ways.

time awk '{printf "%s", $0}' < /etc/termcap > /dev/null
2.3u 0.0s 0:02 85% 55+102k 22+1io 4pf+0w

time awk 'BEGIN{ORS=""}{print}' < /etc/termcap > /dev/null
1.8u 0.0s 0:02 80% 49+100k 22+1io 4pf+0w

time tr -d '\012' < /etc/termcap > /dev/null
1.2u 0.0s 0:01 71% 8+41k 22+0io 3pf+0w

time perl -pe 'chop;' < /etc/termcap > /dev/null
1.0u 0.1s 0:01 65% 169+146k 22+5io 5pf+0w

It looks like perl is the quickest.  Some would argue it's also the
dirtiest...   :-)

I do not believe it is possible with sed except for very short files or
by using multiple passes.  Even then I doubt you could get rid of the
final newline.  Similar problems with ed.

The combination of sed and m4 has possibilities if you know what you're
translating won't interfere with m4.

If all your records are the same size, or you don't mind trailing blanks,
it can also be done with dd.

And you can always write a little echo loop in your shell.

There's probably a way to do it with adb, but I don't want to think about it.

There's also this quaint language called 'C'...

That's all I can think of, offhand, (other than rm, which deletes more than
just newlines).

Larry Wall
lwall@jpl-devvax.jpl.nasa.gov

johnl@esegue.segue.boston.ma.us (John R. Levine) (01/05/90)

In article <990@manta.NOSC.MIL> barajas@manta.NOSC.MIL (Albert A. Barajas) writes:
>Is there a quick and dirty way to delete newlines
>from an ascii text file?

	tr -d \\012 < input > output

you might want to get rid of carriage returns, too:

	tr -d \\012\\015 < input > output
-- 
John R. Levine, Segue Software, POB 349, Cambridge MA 02238, +1 617 864 9650
johnl@esegue.segue.boston.ma.us, {ima|lotus|spdcc}!esegue!johnl
"Now, we are all jelly doughnuts."