[comp.unix.questions] a "trivial" sed question

alen@cogen.UUCP (Alen Shapiro) (06/07/88)

I know the answer is 'use tr -d "\012"' but here is the question;

Is there a way USING SED to remove all <NL> chars from a file. This is
just an exercise since "tr" provides the functionality but it was
part of a question that a student asked me and I was unable to give
a satifactory answer. The effect I want to see can be emulated by
2 sed commands

1) 'N' - add next line to current pattern space
2) 's/\n//g'

But I only get what I want if I repeat 'N' <x> times on the first line
of the script (where <x> is the number of lines in the input file - not
very satisfactory).

The closest I got was to make a script that joins every second line!!

: l
N
s/\n/ /
t l

I have managed to crash "sed -n" with a core dump with this effort...
(sed{0,1}.c 1.1 86/07/07 SMI)

: b
N
s/\n/ /g
H
t b
g
s/\n/ /g
p

which just goes to show how convoluted my reasoning became before giving up.
I bet you guys and gals can think of a really easy solution.


--alen the Lisa slayer (it's a long story)

    ...!{seismo,esosun,suntan}!cogen!alen

leo@philmds.UUCP (Leo de Wit) (06/13/88)

In article <512@cogen.UUCP> alen@cogen.UUCP (Alen Shapiro) writes:
>I know the answer is 'use tr -d "\012"' but here is the question;
>
>Is there a way USING SED to remove all <NL> chars from a file. This is
 [40 lines deleted]

From an sed addict:
I don't think this can be done for any size of file. I'll explain:
Whenever sed outputs a line, it has a trailing newline. The best you can
do is thus create one big line containing all lines of the file and remove
newlines from it (all but the last). You already indicated that you can use
N to add to the pattern space. The problem is: this pattern space has of
course a limited size (don't know if it is malloc'ed or just a big buffer)
unless sed swaps this space to the disk (don't think so). Think your core
dump was due to running out of buffer space. If your file is small enough,
you could do:

sed -n -e '
1h
2,$H
${
	x
	s/\n//g
	p
}' your_file

This doesn't need labels (look Ma, no GOTO's! 8-).

	Leo.