1

On using the command:

tr -s '\n' ' '< test1

where test1 is a text file.

If test1 is written in a manner such that it contains newlines (linefeed characters) and not \n (backslash-n) then the answer comes as expected that is all the newlines get replaced by a single space.

But if say the test1 is formatted such that it contains \n (backslash-n) and not a newline then running the command the \n does not get replaced by a single space, E.g. if test1 contains

hello \n\n\n how are \n\n you

then the output is

hello \n\n\n how are \n\n you

and not

hello   how are   you
codelec
  • 11

1 Answers1

2

So is the question how does one substitute literal backslash-n's?

If so, it can't be done with tr, since tr only works on single characters. Here's how to do it in sed (string editor):

$ cat test1
hello \n\n\n how are \n\n you
$ sed -e 's/\\n/ /g' < test1
hello     how are    you

note the extra backslash needed to tell sed we're looking for a literal backslash here, and to not interpret '\n' as a line feed character.