Substitute \n with carriage return

Hello all,

I've a flat file in the following format:

 
AB\001\CDED\001\ABC\001\nEG\001\HIJF\001\EFG\001\nHI\003\HIUL\003\HIJ\003

And I want to substitute \n with the carriage return. Any help is appreciated!

Regards,

  • Seth
tr '\n' '\r' < input > output

Hi, Thank-you for the response.

Its actually throwing out a control-M at the end of the file.

 
AB\001\CDED\001\ABC\001\nEG\001\HIJF\001\EFG\001\nHI\003\HIUL\003\HIJ\003^M

Are you sure your file is not DOS format?

That ^M character is a Carriage return

But I want to replace

\n

to carriage return. I was hoping to get the output something like this:

 
AB\001\CDED\001\ABC\001
EG\001\HIJF\001\EFG\001
HI\003\HIUL\003\HIJ\003  

awk '{gsub(/\\n/,RS)}1' file

Be aware inside *NIX flavours!

The first in the code prints "Hello World." on the next line immediatley followed by the prompt.
The second with "\n" gives a newline for the prompt.
The third with "\r", (^M), overwrites itself so you see nothing.
Fourth and Fifth, "\r\n", "\n\r", are _effectively_ the same but are NOT.

Carraiage Return does EXACTLY what it says on the tin as does Newline...

Last login: Fri Jun 21 17:53:47 on ttys000
AMIGA:barrywalker~> printf "Hello World."
Hello World.AMIGA:barrywalker~> printf "Hello World.\n"
Hello World.
AMIGA:barrywalker~> printf "Hello World.\r"
AMIGA:barrywalker~> printf "Hello World.\r\n"
Hello World.
AMIGA:barrywalker~> printf "Hello World.\n\r"
Hello World.
AMIGA:barrywalker~> 

I suspect the you need '\r" added and not swapped for Windows reasons...

You don't want to replace \n with <carriage return>, but with the <line feed> (newline, ^J, 0x0A) char to produce your desired output. So use Yoda's proposal. With <CR>, your output would look like

HI\003\HIUL\003\HIJ\003

as the two first lines would be overwritten!