66

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'

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
Alastair
  • 1,710
  • 2
  • 17
  • 33
  • 17
    As @Aniket points out, `tr` won't work for this - it replaces a single byte with a single byte and can't deal with multi-byte strings ("\n"). You'll need `sed`, `awk` or another tool to accomplish this. – twalberg Nov 28 '12 at 17:19
  • Duplicate of https://stackoverflow.com/questions/19762365/sed-help-matching-and-replacing-a-literal-n-not-the-newline – tripleee Aug 29 '21 at 06:48

3 Answers3

98

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
sampson-chen
  • 45,805
  • 12
  • 84
  • 81
  • 29
    This does not work for me on bash 3 on OS X. It replaces \n with n. – rjurney Mar 12 '17 at 02:10
  • 4
    @rjurney You need the GNU version of `sed` for this to work. `brew install gnu-sed` then either use the `gsed` command or set your `$PATH` so that the GNU version takes precedence: https://stackoverflow.com/a/34815955/3316036 – diplosaurus Dec 30 '19 at 22:55
  • The Bash version makes no difference, but the `sed` version does. It is unfortunate that a nonportable version got accepted. – tripleee Aug 29 '21 at 06:22
  • See the proposed duplicate for portability notes and a couple of portable alternatives. – tripleee Aug 29 '21 at 08:59
4

The Perl solution is similar to the sed solution from sampson-chen:

perl -pe 's/\\n/\n/g'

Examples:

Input file with literal \n (not newlines):

$ cat test1.txt          
foo\nbar\n\nbaz

Replace literal all occurrences of \n with actual newlines, print into STDOUT:

$ perl -pe 's/\\n/\n/g' test1.txt
foo
bar

baz

Same, change the input file in-place,saving the backup into test1.txt.bak:

$ perl -i.bak -pe 's/\\n/\n/g' test1.txt

The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-p : Loop over the input one line at a time, assigning it to $_ by default. Add print $_ after each loop iteration.
-i.bak : Edit input files in-place (overwrite the input file). Before overwriting, save a backup copy of the original file by appending to its name the extension .bak.

SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches
perldoc perlre: Perl regular expressions (regexes)

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
0

I had a similar issue where I wanted to replace '\n' with new line on Mac BigSur

Input:

-----BEGIN CERTIFICATE-----\\nMIGcz......\naLRn\\nyxiJdo=\\n-----END CERTIFICATE-----

Expected output:

-----BEGIN CERTIFICATE-----
MIGcz......
aLRn
yxiJdo=
-----END CERTIFICATE-----

Sed, perl none worked for me. But a simple 'tr' command was useful.

echo -----BEGIN CERTIFICATE-----\\nMIGcz......\\naLRn\\nyxiJdo=\\n-----END CERTIFICATE----- | tr '\' '\n'
TheTharun
  • 9
  • 1