3

I want to replace \n with blank

Input file1

A\n
D\n 

Output file2 I want

A       
D  

I tried below commands but not able to replace.

cat OUT | tr '\n' ' '
4
  • 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?
    – Scott
    Mar 10 '15 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 '15 at 8:20
  • are you just trying to convert DOS/Windows file to Unix files?
    – Skaperen
    Mar 20 '15 at 10:56
  • how about this question [stackoverflow.com/questions/2613800/…
    – Skaperen
    Mar 20 '15 at 11:02
5

Many good answers, I'll add the sed way

 sed 's#\\n##g' file1 >  file2
5
  • In awk you'd do something like: awk '{ gsub(/\\n/,"") }1' file Also quite funny you use hashes # as substitution delimiters. Mar 10 '15 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 '15 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 '15 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 '15 at 14:02
  • with awk there are probably a few dozen ways to do this
    – Skaperen
    Mar 20 '15 at 10:58
2

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

$ cut -c1 < input_file 
A
D
2

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

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

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.

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

You can use tr

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

2
  • 6
    This will clobber every \ and every n -- probably not what the OP wants.
    – Scott
    Mar 10 '15 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 '15 at 21:47
-1

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.

2
  • 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 '15 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 '15 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.