This stackoverflow question has an answer to replace newlines with sed, using the format sed ':a;N;$!ba;s/\n/ /g'.
This works, but not for special characters like \r, \n, etc.
What I'm trying to do is to replace the newline character by a literal \n. Tried
sed ':a;N;$!ba;s/\n/\\n/g'
and
sed ':a;N;$!ba;s/\n/\\\n/g'
also
sed ":a;N;$!ba;s/\n/'\'n/g"
but all to no avail. Sed keeps replacing the newline character.... with a newline character.
Thoughts?
For the sake of completeness the commands run are :
PostContent=cat $TextTable | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g'
Where TextTable is a variable linking to a text file containing a JSON output in the following format :
{"posts":[{"title":"mysupertest","slug":"bi-test","markdown":"##TEST First things first ! To TEST this TEST TEST, click the download button below. If you need more information about the TEST TEST, you can read the Table of Contents below. <a href='/assets/TEST.pdf' style='border-radius:5px; padding: 4px 15px; background-color:#008CBA; color:white; text-decoration:none; float:right;' download> Download </a> ##TEST OF TEST ###TEST TEST PLATFORM TEST GUIDE WaTESTve TEST SetupTEST TESTTEST TESTTESTETESTTETSTTEST TESTTESTTTETST TESTTES TESTTESTESSTSTESTESTTES TEST","image":"http://localhost:3000/myimage.jpg","featured":false,"page":false,"status":"draft","language":"en_US","meta_title":null,"meta_description":null,"author":"4","publishedBy":null,"tags":[{"uuid":"ember2034","name":"implementation guides","slug":null,"description":null,"meta_title":null,"meta_description":null,"image":null,"visibility":"public"}]}]}
So: Press CTRL-h and the Replace dialog will open. Type \r\n in "Find what" and \\r\\n in "Replace with". Then select search mode Extended (\r, \n, \t, \x..., \0) and click "Replace All".
Operating systems have special characters denoting the start of a new line. For example, in Linux a new line is denoted by “\n”, also called a Line Feed. In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF.
In the “Find What” box, enter “\n”. In the Replace with box, type the character that you want to replace it with. Make sure “Extended” is selected and click “Replace All,” and your list will go back to being separated by a standard character, such as a comma or pipe.
Using `sed` to replace \n with a comma By default, every line ends with \n when creating a file. The `sed` command can easily split on \n and replace the newline with any character. Another delimiter can be used in place of \n, but only when GNU sed is used.
Is this all you're trying to do?
$ cat file a b c $ awk '{printf "%s\\n", $0}' file a\nb\nc\n$
or even:
$ awk -v ORS='\\n' '1' file a\nb\nc\n$
Run dos2unix on the input file first to strip the \r
s if you like, or use -v RS='\r?\n'
with GNU awk or do sub(/\r$/,"");
before the printf or any other of a dozen or so clear, simple ways to handle it.
sed is for simple substitutions on individual lines, that is all. For anything else you should be using awk.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With