0

I have several text files which should contain text in multiple lines. I can see that the \n is present where the text should go to a new line, but this is not working. I also noticed that there is no space between the \n and the following word (i.e. word1 \nword2) could this be the cause? How can I solve this? I tried to modify manually one of the files (by changing the \n with an actual "return") and this worked, but I have hundreds of files. I would like to use sed, but I don't know how to change \n to "return".

ginopino
  • 380
  • Can you wordcount one or two files: wc filename? It seems whatever wrote these files had a two-byte escape sequence "\n" that failed to be interpreted -- can the files be recreated properly? It can be hard to deal with a file that is one long line. Awk is probably best: read the whole file in one line, gsub the string "\n" to "\n", and write the whole text. – Paul_Pedant Jun 10 '20 at 16:49
  • It appears that you have two literal characters \ and n in your text file rather than the special \n character. Replacing those as per the answer of @Siva would fix the issue in the current files. If you are creating these files yourself, you should adjust how they are being created to ensure they meet your expectation. – einfeyn496 Jun 10 '20 at 16:49
  • Thank you both for your comments. The files were not created by me, thus, it will be impossible to recreate them properly or adjust how they have been created. The solution suggested by @Siva worked! – ginopino Jun 10 '20 at 16:58

1 Answers1

2

Try this (requires GNU sed),

sed -e "s#\\\n#\n#g" file
  • we need to escape the slash to behave it as text.
  • replace the option -e to -i, if the output looks fine
Kusalananda
  • 333,661
Siva
  • 9,077
  • Note that -e and -i are not mutually exclusive. The -e option just means "the next argument is the sed expression". – Kusalananda Jun 12 '20 at 06:43