SUMMARY: How to replace '\n' by ' ' in text files

From: Enrique Vadillo (vadillo@rcp.net.pe)
Date: Fri May 07 1999 - 15:19:41 CDT


Original posting:
-----------------
I have some small 8-bit text files that have newline chars and i would like to
replace all those newline chars by a space ' ' keeping the 8-bit chars intact.
I know i can do this in 1 minute with a C program but for some reason
i want to do this in a shell script. any ideas?

I was overwhelmed by answers, thank you all guys!
some guys suggested i use 'sed' but 'man sed' says sed can not replace \n
here are the replies i received until now, again, thanx:

hchung@cems.umn.edu (Hahn Kyu Chung)
------------------------------------
You can use awk to do it:
        awk 'BEGIN { ORS=" " } { print }' < FILENAME
where FILENAME is the name of the file you are converting.
Alternatively, use perl:
        perl -ne 's/\n/ /g; print' < FILENAME

bzimmer@all-phase.com (Bruce R. Zimmer)
---------------------------------------
cat FileName | tr '\012' ' '

jensen@erim-int.com (Todd Jensen)
---------------------------------
How about:
        cat <file> | tr '\012' '\040'
man ascii gives you ascii values for the (special) characters you want to
replace.

nicky.ayoub@Microchip.COM (Nicky Ayoub)
---------------------------------------
How about using tr?
tr '\012' ' ' < input_file > output_file

phooper@nebs.com (Patrick Hooper)
---------------------------------
Try this.
$ od -c your_file | tr '\n' ' ' > your_new_file
od (octal dump) with the "-c" option will spit out the file showing all
characters in ascii format. Then pipe it through tr (translate). And
output it to a new file. Any of this can be run alone so you can see
the output. od -c|tr '\n' ' ' will output to screen.
The tr piece may not be exact to what is required but it's pretty close.

sanjiv@aryabhat.cs.umsl.edu (Sanjiv K. Bhatia)
----------------------------------------------
Try the following:
        tr '\012' " " < filename

jasonm@vsl.com (Jason Marshall)
-------------------------------
tr -d '\012' would get rid of the \n's, but you want the carriage
returns, not the newlines, to be deleted... Right? Just do a "man ascii"
and put in the octal for the character you want to delete instead of the
\012... Works for me!

rdelaney@bbt.com (Ray Delaney)
------------------------------
perl -> $newline =~ s/\n/ /;
or $newline =~ tr/\n/ /;

------------------------------
Enrique-

-- 
----------------------------------------------
 RCP - Internet Peru      Tel: +51 1 422-4848 
 Dpto de Operaciones      Fax: +51 1 421-8086
----------------------------------------------



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:13:19 CDT