I want to replace \n with blank

Input file1


Output file2 I want


I tried below commands but not able to replace.

cat OUT | tr '\n' ' '
  • 1
    Do you mean that you want to replace all newlines in the file with spaces, turning the entire file into one long, unterminated line? Or do you mean than your file contains backslash (\) characters, and, when one of them is immediately followed by an n, you want to replace that pair of characters with a space? ... ... ... ... ... ... P.S. Why is your input file called OUT? Mar 10, 2015 at 11:34
  • The other answers make things clear, but also in general, tr is for character replacement. Not for string replacement. Also, the question is confusing, I first assumed that you wanted to replace the newlines with spaces, not literal \n with an empty string. Do you want to only do this substitution at the end of the line or anywhere?
    – orion
    Mar 20, 2015 at 8:20
  • are you just trying to convert DOS/Windows file to Unix files?
    – Skaperen
    Mar 20, 2015 at 10:56
  • how about this question [stackoverflow.com/questions/2613800/…
    – Skaperen
    Mar 20, 2015 at 11:02

6 Answers 6


Many good answers, I'll add the sed way

 sed 's#\\n##g' file1 >  file2
  • In awk you'd do something like: awk '{ gsub(/\\n/,"") }1' file Also quite funny you use hashes # as substitution delimiters. Mar 10, 2015 at 10:27
  • 1
    so far you have the only correct answer. +1. if i nitpick a bit, I'd add a $ after the n, if you only want to delete the \n that are at the end of each line Mar 10, 2015 at 12:45
  • @val0x00ff - I use #'s to avoid picket fence looking sed commands when having to escape complexer strings. And thanks for the awk example, i'm trying to get more mileage out of it besides the usual 'default' commands. - olivier-dulac Good remark, i will add the $ anchor in the anser
    – Jake
    Mar 10, 2015 at 13:56
  • hmmm, adding the $ anchor makes it miss the last occurance. doing $* does work but looks a bit too greedy...
    – Jake
    Mar 10, 2015 at 14:02
  • with awk there are probably a few dozen ways to do this
    – Skaperen
    Mar 20, 2015 at 10:58

An alternate would be to take only the first character using cut:

$ cut -c1 < input_file 

From post:

I tried below commands but not able to replace please do needful.

cat OUT | tr '\n' ' '

The problem with with above command is : tr understands \n as "new line character"

From manpage:

$ man tr | grep Interpreted -A 18
       SETs are specified as strings of characters.  Most represent themselves.  Interpreted sequences are:

       \NNN   character with octal value NNN (1 to 3 octal digits)

       \\     backslash

       \a     audible BEL

       \b     backspace

       \f     form feed

       \n     new line

       \r     return

       \t     horizontal tab

       \v     vertical tab

So to specify \n you need to specify [backslash][n] i.e \\n (see specification listed above). Thus Use the following command:

tr -s '\\n' ' '

To cat file1 and redirect(>) to file2 Use:

cat file1 | tr -s '\\n' ' ' > file2

But the problem with using tr is that it translates EVERY \ and n to space.

So, I recommend to use sed as follows:

Command for sed substitution (sed 's/old-string/new-string/g' in > out) :

sed 's/\\n/ /g' file1 > file2

Here only \n (whole word) will be converted into space

  • 1
    Your tr solution will destroy every \ and n in the file, just like this answer. Mar 12, 2015 at 17:11
  • 1
    @joeytwiddle oh! you are right; I revised answer by specifying problem with tr and recommending sed!
    – Pandya
    Mar 13, 2015 at 13:17

If you want to replace the two characters \ and n at the end of every line with a space, use

sed 's/\\n$/ /' < file1

Note that your example output has several spaces after A and a single space after D, but the above command always places a single space.

  • This would be a good answer if the closing ' wasn't missing! Mar 12, 2015 at 17:13
  • @joeytwiddle thx, fixed it!
    – lysium
    Mar 20, 2015 at 8:14
  • We still don't know if the OP wants a space or an empty string.
    – orion
    Mar 20, 2015 at 8:24

You can use tr

tr -d '\\n' < input_file > output_file

  • 6
    This will clobber every \ and every n -- probably not what the OP wants. Mar 10, 2015 at 11:39
  • 2
    Oh, It seems I've been missing something! Thanks for pointing it out. I'm just leaving the answer here so other will know how to NOT do it. Mar 12, 2015 at 21:47

you could try a trick!

First get rid of 'n' at the end of the file:

cat "your_file.txt" | sed -e 's/\<n\>//g' > "new_file.txt"

then get rid of the "\" character:

cat "new_file.txt" | tr -s "\\" " " > "final_file.txt"

and you're going to have the output without the \n.

  • No need for cat here. sed will accept input files as arguments (eg. sed EXPR file), and an input redirection could be used for tr (eg. tr a b < file). Mar 10, 2015 at 23:02
  • This defeats the purpose of having full regular expression capabilities at your disposal. Why use sed and not remove the entire string that's a problem? Secondly, this doesn't even do what you want. tr will replace all occurences of ` in the line with a space. Also your sed` command doesn't check if n is at the end of the line, and needlessly does a global replacement. Why not just sed 's/\\n$//' input.txt > output.txt (or remove "$" if \n can happen anywhere).
    – orion
    Mar 20, 2015 at 8:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .