Home > Software engineering >  How to preserve '\n' character while replacing/substitute string via SED?
How to preserve '\n' character while replacing/substitute string via SED?

Time:09-06

We are trying to convert the Kubernetes service account certificate to a JSON String literal and update the same in a template file like below.

sed -i "s|KUBE_CERT|$(kubectl exec <pod> -c <container-name> -- cat /var/run/secrets/kubernetes.io/serviceaccount/ca.crt | awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}')|"  k8sauth.json

Here below part will fetch the certificate

kubectl exec pod-name -c container-name -- cat /var/run/secrets/kubernetes.io/serviceaccount/ca.crt

Below part will convert the certificate into JSON string literal

awk 'NF {sub(/\r/, ""); printf "%s\n",$0;}'

Converted certificate will look like below.

-----BEGIN CERTIFICATE-----\nMIIE6DCCAtCgAwIBAgIQXz5gLYCvCB3IA11iO9agyzANBgkqhkiG9w0BAQsFADAN\nMQswCQYDVQQDEwJjYTAgFw0yMjA5MDEwNDI1NDVaGA8yMDUyMDkwMTA0MzU0NVow\nDTELMAkGA1UEAxMCY2EwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQC0\n3IbCy0ljXJc VdToHwaZvW/Nm3AyoJZ0vjL/eoA1UMBEsXznGV6R TFAtO7zNb N\n/4JchvgQXSZ32x87eyt30MIJqNmwL51u3Wb2r9/CdjpyhHFuB8PEJ1OrmUd 7xjm\n9 1DzVPJ6knQ6/9x 3tmOEfMRXVOqzs84n1MRJvSpixLAOXvOW9NK/q34aDVlZWM\n0duegfEj3ybo/qCgt4wMK Gc2WMy1Y5KrF0v3n7Nlnie1AmVreSBmK7m3hsW9q4w\npD65U8b/3v5e8CK1GwVz9R/yIIsWkJnAdruoG0PFhKseTJmGcglOrHKs8DsUR24V\nrUFBxjnm8mnmuA7ufQw8mi1WMDMXHMkwxH7j78B9B29IYjx5uz CvZVOiJoAXy70\nCwWKBEmIZyob9HMgPjcDa195HlU2A00osJq/sV0tUygLG9eCvoswNF4GQC0U9GdQ\n8YKU/ucWngxpNCkZFNP7eSkywCA9DjLoVlpb35zmFiXQpdP3dJV/W14GNwdtFepO\nffzJICgLcR39JJ0nTadyZdqtaM2My/9rs/1MefR3HgYFpyh8iSVjzn2QCG87gxZx\nf6wDYfhLuqIQX6rV4MSVU dfOJhlQ4IsSbHpHzlVKNmGO MyU57pCR Ez0YxcgHk\nPHXDWZocHGN/oYevxrRiRuTsoeOagiXxh8eSBf cFwIDAQABo0IwQDAOBgNVHQ8B\nAf8EBAMCAqQwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQU iMU09YI9pYJocbC\n8tqQZCAc/aEwDQYJKoZIhvcNAQELBQADggIBAByFON4rWOak59t9Ogx96uLxkgJ1\nPlGpxK LxE7WokVZFps/zPlqyxsYFeXasyhwT8zqzWFbmusnnMRxemx9DKs6fv4Z\nwaRfOU/YAI7ujxNiDYVhMChQGungMkF81fgfONPcqa23HCK5FlzMLSz 1Lvd296g\n eBke0Q0w3lhxC55R4sFGgi2kXc5zwigSLY2nR/0jDNHi5MbRbb3Qdey48h8/X/o\nwyDI7lT/DCXVAfuRI46D5M k q3dBRm5nMMo/cl5NryW2M4viRx8hVFunmDAdSnN\nZlHJFVDimwexS7HZkJqyUcedIK6oORi8ULuv9yqbZT XzTk/uTb3y33BItHXidtu\nxhJYiydJ8pdGRcfjzDvpukcJWggtq/O97zRvRnKrVswuWIEGyfXYwyIQFMdBsDzd\ndKI/eZ2HIpH2SJ6  N/fnOygtCLzEGMZgm1mPzBnV4C/WFoMlSUrkNiFctMhYz9s\njqjvEJAzkutJ41UI5ibr/yzO0N2fVT336Vt8lfopfewVuROEkEl21 0Wnj7lzbYQ\n j7lFXNX4CftwEMI2DiSDB78RKFs0rSDTxo9ezA0IYtUpoldk8bZbHqetmDLFHmo\nQ7AcJjnA4IEeHdt8ya/aS9 bZqO4z/4L0jRu0/19YRvC51STFew ZaKvUylvynAy\nwx5YmCXc bvZlAhA\n-----END CERTIFICATE-----\n

Then SED command will update the below line inside the k8sauth.json file

"kubernetes_ca_cert": "KUBE_CERT"

But after the replacement, all \n vanishes and replaced automatically by the new line. End result looks like below.

"kubernetes_ca_cert": "-----BEGIN CERTIFICATE----- MIIE6DCCAtCgAwIBAgIQXz5gLYCvCB3IA11iO9agyzANBgkqhkiG9w0BAQsFADAN MQswCQYDVQQDEwJjYTAgFw0yMjA5MDEwNDI1NDVaGA8yMDUyMDkwMTA0MzU0NVow DTELMAkGA1UEAxMCY2EwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQC0 3IbCy0ljXJc VdToHwaZvW/Nm3AyoJZ0vjL/eoA1UMBEsXznGV6R TFAtO7zNb N /4JchvgQXSZ32x87eyt30MIJqNmwL51u3Wb2r9/CdjpyhHFuB8PEJ1OrmUd 7xjm 9 1DzVPJ6knQ6/9x 3tmOEfMRXVOqzs84n1MRJvSpixLAOXvOW9NK/q34aDVlZWM 0duegfEj3ybo/qCgt4wMK Gc2WMy1Y5KrF0v3n7Nlnie1AmVreSBmK7m3hsW9q4w pD65U8b/3v5e8CK1GwVz9R/yIIsWkJnAdruoG0PFhKseTJmGcglOrHKs8DsUR24V rUFBxjnm8mnmuA7ufQw8mi1WMDMXHMkwxH7j78B9B29IYjx5uz CvZVOiJoAXy70 CwWKBEmIZyob9HMgPjcDa195HlU2A00osJq/sV0tUygLG9eCvoswNF4GQC0U9GdQ 8YKU/ucWngxpNCkZFNP7eSkywCA9DjLoVlpb35zmFiXQpdP3dJV/W14GNwdtFepO ffzJICgLcR39JJ0nTadyZdqtaM2My/9rs/1MefR3HgYFpyh8iSVjzn2QCG87gxZx f6wDYfhLuqIQX6rV4MSVU dfOJhlQ4IsSbHpHzlVKNmGO MyU57pCR Ez0YxcgHk PHXDWZocHGN/oYevxrRiRuTsoeOagiXxh8eSBf cFwIDAQABo0IwQDAOBgNVHQ8B Af8EBAMCAqQwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQU iMU09YI9pYJocbC 8tqQZCAc/aEwDQYJKoZIhvcNAQELBQADggIBAByFON4rWOak59t9Ogx96uLxkgJ1 PlGpxK LxE7WokVZFps/zPlqyxsYFeXasyhwT8zqzWFbmusnnMRxemx9DKs6fv4Z waRfOU/YAI7ujxNiDYVhMChQGungMkF81fgfONPcqa23HCK5FlzMLSz 1Lvd296g
 eBke0Q0w3lhxC55R4sFGgi2kXc5zwigSLY2nR/0jDNHi5MbRbb3Qdey48h8/X/o wyDI7lT/DCXVAfuRI46D5M k q3dBRm5nMMo/cl5NryW2M4viRx8hVFunmDAdSnN ZlHJFVDimwexS7HZkJqyUcedIK6oORi8ULuv9yqbZT XzTk/uTb3y33BItHXidtu xhJYiydJ8pdGRcfjzDvpukcJWggtq/O97zRvRnKrVswuWIEGyfXYwyIQFMdBsDzd dKI/eZ2HIpH2SJ6  N/fnOygtCLzEGMZgm1mPzBnV4C/WFoMlSUrkNiFctMhYz9s jqjvEJAzkutJ41UI5ibr/yzO0N2fVT336Vt8lfopfewVuROEkEl21 0Wnj7lzbYQ
 j7lFXNX4CftwEMI2DiSDB78RKFs0rSDTxo9ezA0IYtUpoldk8bZbHqetmDLFHmo Q7AcJjnA4IEeHdt8ya/aS9 bZqO4z/4L0jRu0/19YRvC51STFew ZaKvUylvynAy wx5YmCXc bvZlAhA
-----END CERTIFICATE----- "

However, it should look like below

"kubernetes_ca_cert": "-----BEGIN CERTIFICATE-----\nMIIE6DCCAtCgAwIBAgIQXz5gLYCvCB3IA11iO9agyzANBgkqhkiG9w0BAQsFADAN\nMQswCQYDVQQDEwJjYTAgFw0yMjA5MDEwNDI1NDVaGA8yMDUyMDkwMTA0MzU0NVow\nDTELMAkGA1UEAxMCY2EwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQC0\n3IbCy0ljXJc VdToHwaZvW/Nm3AyoJZ0vjL/eoA1UMBEsXznGV6R TFAtO7zNb N\n/4JchvgQXSZ32x87eyt30MIJqNmwL51u3Wb2r9/CdjpyhHFuB8PEJ1OrmUd 7xjm\n9 1DzVPJ6knQ6/9x 3tmOEfMRXVOqzs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SJ6  N/fnOygtCLzEGMZgm1mPzBnV4C/WFoMlSUrkNiFctMhYz9s\njqjvEJAzkutJ41UI5ibr/yzO0N2fVT336Vt8lfopfewVuROEkEl21 0Wnj7lzbYQ\n j7lFXNX4CftwEMI2DiSDB78RKFs0rSDTxo9ezA0IYtUpoldk8bZbHqetmDLFHmo\nQ7AcJjnA4IEeHdt8ya/aS9 bZqO4z/4L0jRu0/19YRvC51STFew ZaKvUylvynAy\nwx5YmCXc bvZlAhA\n-----END CERTIFICATE-----\n"

Please help to achieve this.

CodePudding user response:

sed interprets \n as a newline. You have to escape \ if you want it to be taken literally.

kube_cert=$(
   kubectl exec <pod> -c <container-name> -- cat /var/run/secrets/kubernetes.io/serviceaccount/ca.crt |
   awk 'NF {
      sub(/\r/, "")
      printf "%s\\\\n", $0;
   }'
)

sed -i "s|KUBE_CERT|$kube_cert|"  k8sauth.json
  • Related