6

How can I convert a file with multiple lines to a string with \n characters in bash?

For example - I have a certificate that I need to configure in my configuration JSON file so instead of having

-----BEGIN CERTIFICATE-----
MIIDBjCCMIIDB
MIIDBjCCMIIDB
    .... 
MIIDBjCCMIIDB==
-----END CERTIFICATE-----

I will have

-----BEGIN CERTIFICATE-----\nMIIDBjCCMIIDB\nMIIDBjCCMIIDB\n....\nMIIDBjCCMIIDB==\n-----END CERTIFICATE-----
3
  • Read the file line by line and append the read data to a variable
    – msrd0
    Oct 19, 2014 at 14:34
  • @AvinashRaj - I admit my example is not accurate. the ellipsis represents the content in between the line before that and the line after that. seems to me like the question is clear enough as I did not get bad feedabck. Oct 20, 2014 at 7:04
  • your expected output shows your needs but How can I convert a file with multiple lines to a string with \n characters in bash? line shows a different thing. Oct 20, 2014 at 7:05

4 Answers 4

11

One way using awk:

$ awk '$1=$1' ORS='\\n' file
-----BEGIN CERTIFICATE-----\nMIIDBjCCMIIDB\nMIIDBjCCMIIDB\n....\nMIIDBjCCMIIDB==\n-----END CERTIFICATE-----\n
1
  • If this appears to not work, double check your file's line endings. You can assert the line endings are unix-friendly by by first running the file through dos2unix. A oneliner solution looks like: cat file | dos2unix | awk '$1=$1' ORS='\\n' /dev/stdin Nov 1, 2019 at 19:36
1

A pure bash (with Bash≥4) possibility that should be rather efficient:

mapfile -t lines_ary < file
printf -v cert '%s\\n' "${lines_ary[@]}"

Check that it works:

echo "$cert"

One thing to note is that you will have a trailing \n. If that's not a concern, you're good with this method. Otherwise, you may get rid of it by adding the following line just after the printf -v statement:

cert=${cert%\\n}
1

Bash has simple string substitution.

cert=$(cat file)
echo "${cert//$'\n'/\\n}"

I originally had '\n' in single quotes in the substitution part, but I took them out based on testing on Bash 3.2.39(1) (yeah, that's kinda old).

2
  • this is what i needed! just a correction... it should be \\n and not \n. else it wont work.. so it becomes ===> echo "${cert//$'\n'/\\n}"
    – Macindows
    Aug 24, 2020 at 15:31
  • @Macindows Works both ways for me, but I added a second backslash based on your feedback. I'm still on Bash 3.2.57(1) here (stock macOS Catalina). Thanks.
    – tripleee
    Aug 24, 2020 at 15:36
0

Another awk solution,

$ awk -v RS= '{gsub(/\n+/, "\\n")}1' file
-----BEGIN CERTIFICATE-----\nMIIDBjCCMIIDB\nMIIDBjCCMIIDB\n    .... \nMIIDBjCCMIIDB==\n-----END CERTIFICATE-----

Through sed,

$ sed ':a;N;$!ba;s/\n/\\n/g' file
-----BEGIN CERTIFICATE-----\nMIIDBjCCMIIDB\nMIIDBjCCMIIDB\n    .... \nMIIDBjCCMIIDB==\n-----END CERTIFICATE-----

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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