Newbie: delete end of line with sed

Newbie: delete end of line with sed

Post by jose_luis_fdez_diaz_n.. » Sat, 24 Jun 2006 01:19:09



Hi,

How can I delete the end of line character with sed?

Thanks in advance,
Jose Luis.

 
 
 

Newbie: delete end of line with sed

Post by Willia » Sat, 24 Jun 2006 02:41:23



Quote:

> Hi,

> How can I delete the end of line character with sed?

tr would be a better choice in most cases:

tr -d '\012' < filein > fileout

-Wm

 
 
 

Newbie: delete end of line with sed

Post by Steffen Schule » Sat, 24 Jun 2006 06:32:59



> Hi,

> How can I delete the end of line character with sed?

> Thanks in advance,
> Jose Luis.

With GNU sed:

sed ':a;N;$!ba;s/\n//g'

This deletes all linefeeds. Only the last one isn't deleted.

Cheers,

Steffen

 
 
 

Newbie: delete end of line with sed

Post by Stephane CHAZELA » Sat, 24 Jun 2006 16:19:53


2006-06-22, 23:32(+02), Steffen Schuler:

>> Hi,

>> How can I delete the end of line character with sed?

>> Thanks in advance,
>> Jose Luis.

> With GNU sed:

> sed ':a;N;$!ba;s/\n//g'

> This deletes all linefeeds. Only the last one isn't deleted.

[...]

With other seds:

sed -e :a -e 'N;$!ba' -e 's/\n//g'

But as it stores the whole file in the pattern space and the
pattern space generally has a limit on its size, it would only
work for small files.

In any case, sed is not the right tool for the task. Either use:

tr -d '\n' < "$file"

or, if you want to keep the last newline as with the sed
solution:

paste -sd '\0' - < "$file"

Or:

awk '{printf "%s", $0} END {if (NR>0) print ""}' < "$file"

--
Stphane

 
 
 

Newbie: delete end of line with sed

Post by anju » Sat, 24 Jun 2006 16:22:41




> > Hi,

> > How can I delete the end of line character with sed?

> > Thanks in advance,
> > Jose Luis.

> With GNU sed:

> sed ':a;N;$!ba;s/\n//g'

> This deletes all linefeeds. Only the last one isn't deleted.

> Cheers,

> Steffen

Hi Steffen,

           What is this " :a;N;$!ba;" in sed ':a;N;$!ba;s/\n//g'.
Why we are using that ..

Please, clarify my doubt...

Thanks,
an

 
 
 

Newbie: delete end of line with sed

Post by Stephane CHAZELA » Sat, 24 Jun 2006 16:46:58


2006-06-23, 00:22(-07), anju:
[...]
Quote:>> sed ':a;N;$!ba;s/\n//g'

>> This deletes all linefeeds. Only the last one isn't deleted.

>> Cheers,

>> Steffen

> Hi Steffen,

>            What is this " :a;N;$!ba;" in sed ':a;N;$!ba;s/\n//g'.
> Why we are using that ..

[...]

sed is a tool that processes some input one line at a time. The
list of commands you pass it is to process its pattern space,
which is fed the "current line". So the sed commands (:, b,
s...) never see the newline characters.

The above bypasses the implicite loop sed does over the input by
creating a new loop that instead of filling the pattern space
with the current line, in turn, fill the pattern space with all
the lines with newline characters in between (that's what N
does). When the last line is reached, we get out of the loop and
delete all the newline characters from the pattern space (which
is printed by default followed by a newline character).

See man sed about the ":" and "b" branching commands and N
command.

--
Stphane

 
 
 

Newbie: delete end of line with sed

Post by romy » Wed, 28 Jun 2006 05:01:05


You can use :

sed '$d' filename

Reg,
romy.


> Hi,

> How can I delete the end of line character with sed?

> Thanks in advance,
> Jose Luis.