Retain \n as plain text instead of new line in bash script
Andrew Mclaughlin
I have an existing text file with multiple lines. I am trying to build a bash script that concatenates all these lines into one single line delimited by \n (actual text and not new line) to pass it as a field in JSON file for an API to consume.
Source file:
abc=123
def=456
ghi=789Expected output:
abc=123\ndef=456\nghi=789Whatever I tried is either removing trailing new lines or returning same output as source file. Is it possible to tell script to treat \n plain text instead of new line? Appreciate any inputs as I am not able to find anything helpful online.
2 Answers
sed 's/$/\\n' will add literal \n at the end of every line. Then you need tr -d '\n' to remove actual newline characters.
If the last line of your input file is not terminated by a newline character then sed may or may not process it. If the last line is properly terminated then our sed will add \n there as well. Your example shows you don't want \n at the end; therefore we need to modify our sed command slightly. The two commands together will be like:
<input sed '$ ! s/$/\\n/' | tr -d '\n'Because our tr removes all newline characters, there will be no terminating newline character in the output. If you want one then simply run echo afterwards:
<input sed '$ ! s/$/\\n/' | tr -d '\n'; echoIf you want to capture the output with $() then note it removes all trailing newline characters; so in this case the additional echo makes no difference.
Here's a short perl solution to the problem
perl -e 'chomp(@a=<>); print join ("\\n", @a), "\n"'You can feed it from stdin or supply files on the end of the command
Example
( echo one; echo two; echo three ) | perl -e 'chomp(@a=<>); print join ("\\n", @a), "\n"'
one\ntwo\nthreeThat example is memory-bound as it will read its entire stdin into memory before writing it out. If you have an extremely large file you may prefer a looped approach. The same rules apply for reading input as above.
perl -e 'while (<>) { print "\\n" if $a++; chomp; print }; print "\n"'