[oclug] sed/tr question

Brenda J. Butler bjb at istop.com
Fri Mar 18 04:16:44 EST 2005



On Thu, Mar 17, 2005 at 10:38:49PM +0000, hdc at fs.ca wrote:
> I am completely stumped - I have a DOS ASCII file comprised of a lot of short 
> lines ending in the familiar 0D 0A combination.
> 
> I need to combine these lines into a single line (and then perform other 
> manipulations on the data), so I am trying to use sed and/or tr to remove the 
> CR/LF combination.  
> 
> I can run them through sed and remove the 0D characters (sed -e 's/.$//'), but 
> I can't lose the hex 0A character to save my life.  I have trolled through 
> every tutorial/help file on the Net that I could find, but no joy.

The problem with using sed for manipulating newlines is that
sed is a line-delimited editor.

As sed processes the lines, it actually throws away the newline,
and when it prints output it adds it back.  So nothing you do
to the pattern or hold spaces to get rid of the newlines will have
any effect:  newlines will be added for each cycle.

So, you have to get sed to accumulate the whole file in pattern or
hold space, then remove the embedded newlines and carriage returns,
and print out the result (which will have a single newline at the end):

sed -n 'H; ${g; s/\n//g; s/\r/ /g; p}' infile

\n => newline
\r => carriage return

It's usually easier to use another tool for removing newlines from
files, as Patrick and Stephen pointed out.

cheerio,
bjb


More information about the OCLUG mailing list