Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tr command - how to replace the string "\n" with an actual newline (\n) [closed]

Tags:

linux

gnu

tr

I would like to use the tr command to replace all occurrences of the string "\n" with a new line (\n).

I tried tr '\\n' '\n' but this just seems to match any '\' and any 'n'

like image 988
Alastair Avatar asked Nov 28 '12 17:11

Alastair


People also ask

What is newline character Linux?

The newline character is denoted as “\n”. Using both the echo and printf commands, we can print strings with new lines in them.


1 Answers

Here's how to do it with sed:

sed 's/\\n/\n/g' 

Example usage:

To replace all occurrences of \n in a file in-place:

sed -i 's/\\n/\n/g' input_filename 

To replace all occurrences of \n through a pipe, and save into another file

cat file1 file2 file3 file4 | sed 's/\\n/\n/g' > output_file 
like image 115
sampson-chen Avatar answered Oct 02 '22 05:10

sampson-chen