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".
Asked
Active
Viewed 1,033 times
0
ginopino
- 380
1 Answers
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
-eto-i, if the output looks fine
Kusalananda
- 333,661
Siva
- 9,077
-
Note that
-eand-iare not mutually exclusive. The-eoption just means "the next argument is thesedexpression". – Kusalananda Jun 12 '20 at 06:43
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\andnin your text file rather than the special\ncharacter. 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