sed and newline (x0a)

sed and newline (x0a)

am 22.07.2005 19:13:01 von laussy

Hi,

I would like to remove some of the carriage return (newline characters) in a
file; those are characters which hex code is 0a. I need remove only some of
them depending on the sentence they apply to, so tr -d '\n' would not do.

Sed should be all right but for unclear reasons to me it fails to make use
of '\n' properly, so that for instance

sed -e 's/\n//' file

won't work. Why is that?

Cheers,

F.

Re: sed and newline (x0a)

am 22.07.2005 20:10:24 von johngnub

A few more tricks to try:
`grep . foo.txt | tr '\012' '~' `
Change the oct code for the LF , line feed, ( same as hex 0a ) to a "~"
for fun, so we can see what is being changed or not changed. And when
we are happy with the output then:
`grep . foo.txt |tr -d '\012' `

Or we can use perl on a pipe maybe, A one line perl that I have used
in the past:
grep . bad.txt |perl -lane '$line=$_; $line =~ s/\n+//g; print "$line"
'
Reads as, output char from a file named bad.txt to a pipe, to perl with
-lane options, set a var named line from the stdin then bind a sub to
the var line, find any metachar of cr, replace that with nothing, then
print the var. Note the use of single quotes for the perl syntax.

One last question, are you sure you have a "\n" char? and not a '\015'
( 0d ) ? Try grep . foo.txt |od -ta. That would have kept your sed from
working right, No?

Post a line for all to see.

Ponders the JB.

Re: sed and newline (x0a)

am 22.07.2005 20:24:31 von Icarus Sparry

On 2005-07-22, F.P. Laussy wrote:
> I would like to remove some of the carriage return (newline characters) in a
> file; those are characters which hex code is 0a. I need remove only some of
> them depending on the sentence they apply to, so tr -d '\n' would not do.

(Sidenote
Most people would assert that in ASCII carriage return has a hex code
of 0d, and this is often accessible in C as '\r'. Linefeed has a code
of 0a. Newline is a logical concept in Unix, and is often implemented
as a linefeed.)

> Sed should be all right but for unclear reasons to me it fails to make use
> of '\n' properly, so that for instance
>
> sed -e 's/\n//' file
>
> won't work. Why is that?

Because sed is a line oriented program. A reasonable implementation
strategy for sed is to strip off newline characters on input, and
add them back on on output.

You probably need to look at commands like 'N' in sed, or tell us more
about your problem.

Re: sed and newline (x0a)

am 22.07.2005 21:50:30 von someone

johngnub wrote:
> A few more tricks to try:
> `grep . foo.txt | tr '\012' '~' `
> Change the oct code for the LF , line feed, ( same as hex 0a ) to a "~"
> for fun, so we can see what is being changed or not changed. And when
> we are happy with the output then:
> `grep . foo.txt |tr -d '\012' `
>
> Or we can use perl on a pipe maybe, A one line perl that I have used
> in the past:
> grep . bad.txt |perl -lane '$line=$_; $line =~ s/\n+//g; print "$line"'

You are using the -a option which tells perl to split the line on
whitespace and put the non-whitespace parts in the @F array but you are
not using the @F array.

You are using the -l option which tells perl to chomp (remove newline at
EOL) the line and set the OUTPUT RECORD SEPARATOR to "\n".

Since your program reads a line at a time (the -n option) and the -l
option removes the newline there is nothing for the substitution to find
and since the ORS is automatically printed at the end your program is
effectively doing nothing.



John

Re: sed and newline (x0a)

am 22.07.2005 23:09:34 von johngnub

Hehehe; So thats why it worked so well!

JB

Re: sed and newline (x0a)

am 22.07.2005 23:31:32 von Michael Tosch

F.P. Laussy wrote:
> Hi,
>
> I would like to remove some of the carriage return (newline characters) in a
> file; those are characters which hex code is 0a. I need remove only some of
> them depending on the sentence they apply to, so tr -d '\n' would not do.
>
> Sed should be all right but for unclear reasons to me it fails to make use
> of '\n' properly, so that for instance
>
> sed -e 's/\n//' file
>
> won't work. Why is that?
>
> Cheers,
>
> F.

This only works after an N command:

sed '/search/{N;s/\n//;}' file

strips a \n off if the regexp "search" is found.
However, the next line is left as is, even if it contains "search".

awk is easier :

awk '/search/{printf "%s",$0;next}{print}' file

or

awk '{printf "%s",$0}!/search/{print ""}' file


--
Michael Tosch @ hp : com

Re: sed and newline (x0a)

am 23.07.2005 00:12:59 von cfajohnson

On 2005-07-22, F.P. Laussy wrote:
> Hi,
>
> I would like to remove some of the carriage return (newline characters) in a
> file; those are characters which hex code is 0a. I need remove only some of
> them depending on the sentence they apply to, so tr -d '\n' would not do.
>
> Sed should be all right but for unclear reasons to me it fails to make use
> of '\n' properly, so that for instance
>
> sed -e 's/\n//' file
>
> won't work. Why is that?

Because the newline is not part of the line you have read.

This is a job for AWK. Replace PATTERN with your search criterion;
use multiple lines if necessary:

awk '
/PATTERN/ { printf "%s", $0; next }
{print}
'

--
Chris F.A. Johnson
============================================================ ======
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress