Skip to content
Advertisement

How to convert multiline file into a string in bash with newline character?

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-----nMIIDBjCCMIIDBnMIIDBjCCMIIDBn....nMIIDBjCCMIIDB==n-----END CERTIFICATE-----

Advertisement

Answer

One way using awk:

$ awk '$1=$1' ORS='\n' file
-----BEGIN CERTIFICATE-----nMIIDBjCCMIIDBnMIIDBjCCMIIDBn....nMIIDBjCCMIIDB==n-----END CERTIFICATE-----n
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement