Need help using sed to stop interpreting n as new line in linux bash scripts












1















I am new to linux and any bash scripts and have the following problem:



I have this kryptokey:



-----BEGIN CERTIFICATE-----n
MIICSTCCAfCgAwIBAgIRAMsLZqD4PavC7NJz7+5ld+EwCgYIKoZIzj0EAwIwdjELn
MAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBGn
cmFuY2lzY28xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHzAdBgNVBAMTFnRsn
c2NhLm9yZzEuZXhhbXBsZS5jb20wHhcNMTgxMjMxMTA1ODA5WhcNMjgxMjI4MTA1n
ODA5WjB2MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEn
BxMNU2FuIEZyYW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEfMB0Gn
A1UEAxMWdGxzY2Eub3JnMS5leGFtcGxlLmNvbTBZMBMGByqGSM49AgEGCCqGSM49n
AwEHA0IABEbH7l3CiqLA4N4wgfilYgyEuxDrMAqDX6BrFOfWhymNosjh5FlJDHtNn
GPDKhjtrI6e1q0NC0l6wh9h9TrBn7N2jXzBdMA4GA1UdDwEB/wQEAwIBpjAPBgNVn
HSUECDAGBgRVHSUAMA8GA1UdEwEB/wQFMAMBAf8wKQYDVR0OBCIEIH7OaekSLJdan
S0yuV9PCsuasGTt/+/35aVBXTVbII2rCMAoGCCqGSM49BAMCA0cAMEQCIEd+YP/6n
tCzG/LueYTEio8ApQSyz94ju07pmc3LZJDKBAiALu66LKhOpKhogY9XEFg4TScOtn
el4dC6OnMMTmRsEtoA==n-----END CERTIFICATE-----n


saved in a file $replacementOrg1 (is the path to that file).
Now I want to replace in a template $file "INSERT_ORG1_CA_CERT" with this certificate and safe it in $org1. But I need to keep the "n" Character.
The result should keep the n and write it into one line.



I already tried:



sed -e "s@INSERT_ORG1_CA_CERT@$(cat $replacementOrg1)@g" $file > $org1


but it interprets the "n" as new line.



So the final Output should look like this, 1 String in 1 Line:



    "-----BEGIN   CERTIFICATE-----nMIICSTCCAfCgAwIBAgIRAMsLZqD4PavC7NJz7+5ld+EwCgYIKoZIzj0EAwIw       djELnMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBGncmFuY2lzY28xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHzAdBgNVBAMTFnRsnc2NhLm9yZzEuZXhhbXBsZS5jb20wHhcNMTgxMjMxMTA1ODA5WhcNMjgxMjI4MTA1nODA5WjB2MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEnBxMNU2FuIEZyYW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEfMB0GnA1UEAxMWdGxzY2Eub3JnMS5leGFtcGxlLmNvbTBZMBMGByqGSM49AgEGCCqGSM49nAwEHA0IABEbH7l3CiqLA4N4wgfilYgyEuxDrMAqDX6BrFOfWhymNosjh5FlJDHtNnGPDKhjtrI6e1q0NC0l6wh9h9TrBn7N2jXzBdMA4GA1UdDwEB/wQEAwIBpjAPBgNVnHSUECDAGBgRVHSUAMA8GA1UdEwEB/wQFMAMBAf8wKQYDVR0OBCIEIH7OaekSLJdanS0yuV9PCsuasGTt/+/35aVBXTVbII2rCMAoGCCqGSM49BAMCA0cAMEQCIEd+YP/6ntCzG/LueYTEio8ApQSyz94ju07pmc3LZJDKBAiALu66LKhOpKhogY9XEFg4TScOtn
el4dC6OnMMTmRsEtoA==n-----END CERTIFICATE-----n"


Anybody can help?
Thank you










share|improve this question

























  • Prefix your code/data with four white spaces. Please take a look at editing-help.

    – Cyrus
    Dec 31 '18 at 11:22






  • 2





    I don't get you, n IS a newline.

    – Toto
    Dec 31 '18 at 11:22






  • 1





    sed doesn't permit the substitution string to straddle newlines without additional escaping. cat inserts the contents of a file, not the contents of a variable What exactly do you hope for your code to accomplish, and have you searched for similar questions already? This is a fairly common task.

    – tripleee
    Dec 31 '18 at 11:41











  • If the original CERT is in a file, sed /@INSERT_ORIGINAL_CERT@/{r certfile; d;}' file might work.

    – tripleee
    Dec 31 '18 at 12:01











  • That is not a valid key. What someone has done is "half-encoding" (I don't know a better term) the newlines - they have added the literal string "n" before every newline. What you very likely want is either the original key with no "n" strings or a single line string where every newline has been replaced with "n".

    – l0b0
    Dec 31 '18 at 19:47
















1















I am new to linux and any bash scripts and have the following problem:



I have this kryptokey:



-----BEGIN CERTIFICATE-----n
MIICSTCCAfCgAwIBAgIRAMsLZqD4PavC7NJz7+5ld+EwCgYIKoZIzj0EAwIwdjELn
MAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBGn
cmFuY2lzY28xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHzAdBgNVBAMTFnRsn
c2NhLm9yZzEuZXhhbXBsZS5jb20wHhcNMTgxMjMxMTA1ODA5WhcNMjgxMjI4MTA1n
ODA5WjB2MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEn
BxMNU2FuIEZyYW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEfMB0Gn
A1UEAxMWdGxzY2Eub3JnMS5leGFtcGxlLmNvbTBZMBMGByqGSM49AgEGCCqGSM49n
AwEHA0IABEbH7l3CiqLA4N4wgfilYgyEuxDrMAqDX6BrFOfWhymNosjh5FlJDHtNn
GPDKhjtrI6e1q0NC0l6wh9h9TrBn7N2jXzBdMA4GA1UdDwEB/wQEAwIBpjAPBgNVn
HSUECDAGBgRVHSUAMA8GA1UdEwEB/wQFMAMBAf8wKQYDVR0OBCIEIH7OaekSLJdan
S0yuV9PCsuasGTt/+/35aVBXTVbII2rCMAoGCCqGSM49BAMCA0cAMEQCIEd+YP/6n
tCzG/LueYTEio8ApQSyz94ju07pmc3LZJDKBAiALu66LKhOpKhogY9XEFg4TScOtn
el4dC6OnMMTmRsEtoA==n-----END CERTIFICATE-----n


saved in a file $replacementOrg1 (is the path to that file).
Now I want to replace in a template $file "INSERT_ORG1_CA_CERT" with this certificate and safe it in $org1. But I need to keep the "n" Character.
The result should keep the n and write it into one line.



I already tried:



sed -e "s@INSERT_ORG1_CA_CERT@$(cat $replacementOrg1)@g" $file > $org1


but it interprets the "n" as new line.



So the final Output should look like this, 1 String in 1 Line:



    "-----BEGIN   CERTIFICATE-----nMIICSTCCAfCgAwIBAgIRAMsLZqD4PavC7NJz7+5ld+EwCgYIKoZIzj0EAwIw       djELnMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBGncmFuY2lzY28xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHzAdBgNVBAMTFnRsnc2NhLm9yZzEuZXhhbXBsZS5jb20wHhcNMTgxMjMxMTA1ODA5WhcNMjgxMjI4MTA1nODA5WjB2MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEnBxMNU2FuIEZyYW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEfMB0GnA1UEAxMWdGxzY2Eub3JnMS5leGFtcGxlLmNvbTBZMBMGByqGSM49AgEGCCqGSM49nAwEHA0IABEbH7l3CiqLA4N4wgfilYgyEuxDrMAqDX6BrFOfWhymNosjh5FlJDHtNnGPDKhjtrI6e1q0NC0l6wh9h9TrBn7N2jXzBdMA4GA1UdDwEB/wQEAwIBpjAPBgNVnHSUECDAGBgRVHSUAMA8GA1UdEwEB/wQFMAMBAf8wKQYDVR0OBCIEIH7OaekSLJdanS0yuV9PCsuasGTt/+/35aVBXTVbII2rCMAoGCCqGSM49BAMCA0cAMEQCIEd+YP/6ntCzG/LueYTEio8ApQSyz94ju07pmc3LZJDKBAiALu66LKhOpKhogY9XEFg4TScOtn
el4dC6OnMMTmRsEtoA==n-----END CERTIFICATE-----n"


Anybody can help?
Thank you










share|improve this question

























  • Prefix your code/data with four white spaces. Please take a look at editing-help.

    – Cyrus
    Dec 31 '18 at 11:22






  • 2





    I don't get you, n IS a newline.

    – Toto
    Dec 31 '18 at 11:22






  • 1





    sed doesn't permit the substitution string to straddle newlines without additional escaping. cat inserts the contents of a file, not the contents of a variable What exactly do you hope for your code to accomplish, and have you searched for similar questions already? This is a fairly common task.

    – tripleee
    Dec 31 '18 at 11:41











  • If the original CERT is in a file, sed /@INSERT_ORIGINAL_CERT@/{r certfile; d;}' file might work.

    – tripleee
    Dec 31 '18 at 12:01











  • That is not a valid key. What someone has done is "half-encoding" (I don't know a better term) the newlines - they have added the literal string "n" before every newline. What you very likely want is either the original key with no "n" strings or a single line string where every newline has been replaced with "n".

    – l0b0
    Dec 31 '18 at 19:47














1












1








1


1






I am new to linux and any bash scripts and have the following problem:



I have this kryptokey:



-----BEGIN CERTIFICATE-----n
MIICSTCCAfCgAwIBAgIRAMsLZqD4PavC7NJz7+5ld+EwCgYIKoZIzj0EAwIwdjELn
MAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBGn
cmFuY2lzY28xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHzAdBgNVBAMTFnRsn
c2NhLm9yZzEuZXhhbXBsZS5jb20wHhcNMTgxMjMxMTA1ODA5WhcNMjgxMjI4MTA1n
ODA5WjB2MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEn
BxMNU2FuIEZyYW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEfMB0Gn
A1UEAxMWdGxzY2Eub3JnMS5leGFtcGxlLmNvbTBZMBMGByqGSM49AgEGCCqGSM49n
AwEHA0IABEbH7l3CiqLA4N4wgfilYgyEuxDrMAqDX6BrFOfWhymNosjh5FlJDHtNn
GPDKhjtrI6e1q0NC0l6wh9h9TrBn7N2jXzBdMA4GA1UdDwEB/wQEAwIBpjAPBgNVn
HSUECDAGBgRVHSUAMA8GA1UdEwEB/wQFMAMBAf8wKQYDVR0OBCIEIH7OaekSLJdan
S0yuV9PCsuasGTt/+/35aVBXTVbII2rCMAoGCCqGSM49BAMCA0cAMEQCIEd+YP/6n
tCzG/LueYTEio8ApQSyz94ju07pmc3LZJDKBAiALu66LKhOpKhogY9XEFg4TScOtn
el4dC6OnMMTmRsEtoA==n-----END CERTIFICATE-----n


saved in a file $replacementOrg1 (is the path to that file).
Now I want to replace in a template $file "INSERT_ORG1_CA_CERT" with this certificate and safe it in $org1. But I need to keep the "n" Character.
The result should keep the n and write it into one line.



I already tried:



sed -e "s@INSERT_ORG1_CA_CERT@$(cat $replacementOrg1)@g" $file > $org1


but it interprets the "n" as new line.



So the final Output should look like this, 1 String in 1 Line:



    "-----BEGIN   CERTIFICATE-----nMIICSTCCAfCgAwIBAgIRAMsLZqD4PavC7NJz7+5ld+EwCgYIKoZIzj0EAwIw       djELnMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBGncmFuY2lzY28xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHzAdBgNVBAMTFnRsnc2NhLm9yZzEuZXhhbXBsZS5jb20wHhcNMTgxMjMxMTA1ODA5WhcNMjgxMjI4MTA1nODA5WjB2MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEnBxMNU2FuIEZyYW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEfMB0GnA1UEAxMWdGxzY2Eub3JnMS5leGFtcGxlLmNvbTBZMBMGByqGSM49AgEGCCqGSM49nAwEHA0IABEbH7l3CiqLA4N4wgfilYgyEuxDrMAqDX6BrFOfWhymNosjh5FlJDHtNnGPDKhjtrI6e1q0NC0l6wh9h9TrBn7N2jXzBdMA4GA1UdDwEB/wQEAwIBpjAPBgNVnHSUECDAGBgRVHSUAMA8GA1UdEwEB/wQFMAMBAf8wKQYDVR0OBCIEIH7OaekSLJdanS0yuV9PCsuasGTt/+/35aVBXTVbII2rCMAoGCCqGSM49BAMCA0cAMEQCIEd+YP/6ntCzG/LueYTEio8ApQSyz94ju07pmc3LZJDKBAiALu66LKhOpKhogY9XEFg4TScOtn
el4dC6OnMMTmRsEtoA==n-----END CERTIFICATE-----n"


Anybody can help?
Thank you










share|improve this question
















I am new to linux and any bash scripts and have the following problem:



I have this kryptokey:



-----BEGIN CERTIFICATE-----n
MIICSTCCAfCgAwIBAgIRAMsLZqD4PavC7NJz7+5ld+EwCgYIKoZIzj0EAwIwdjELn
MAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBGn
cmFuY2lzY28xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHzAdBgNVBAMTFnRsn
c2NhLm9yZzEuZXhhbXBsZS5jb20wHhcNMTgxMjMxMTA1ODA5WhcNMjgxMjI4MTA1n
ODA5WjB2MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEn
BxMNU2FuIEZyYW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEfMB0Gn
A1UEAxMWdGxzY2Eub3JnMS5leGFtcGxlLmNvbTBZMBMGByqGSM49AgEGCCqGSM49n
AwEHA0IABEbH7l3CiqLA4N4wgfilYgyEuxDrMAqDX6BrFOfWhymNosjh5FlJDHtNn
GPDKhjtrI6e1q0NC0l6wh9h9TrBn7N2jXzBdMA4GA1UdDwEB/wQEAwIBpjAPBgNVn
HSUECDAGBgRVHSUAMA8GA1UdEwEB/wQFMAMBAf8wKQYDVR0OBCIEIH7OaekSLJdan
S0yuV9PCsuasGTt/+/35aVBXTVbII2rCMAoGCCqGSM49BAMCA0cAMEQCIEd+YP/6n
tCzG/LueYTEio8ApQSyz94ju07pmc3LZJDKBAiALu66LKhOpKhogY9XEFg4TScOtn
el4dC6OnMMTmRsEtoA==n-----END CERTIFICATE-----n


saved in a file $replacementOrg1 (is the path to that file).
Now I want to replace in a template $file "INSERT_ORG1_CA_CERT" with this certificate and safe it in $org1. But I need to keep the "n" Character.
The result should keep the n and write it into one line.



I already tried:



sed -e "s@INSERT_ORG1_CA_CERT@$(cat $replacementOrg1)@g" $file > $org1


but it interprets the "n" as new line.



So the final Output should look like this, 1 String in 1 Line:



    "-----BEGIN   CERTIFICATE-----nMIICSTCCAfCgAwIBAgIRAMsLZqD4PavC7NJz7+5ld+EwCgYIKoZIzj0EAwIw       djELnMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBGncmFuY2lzY28xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHzAdBgNVBAMTFnRsnc2NhLm9yZzEuZXhhbXBsZS5jb20wHhcNMTgxMjMxMTA1ODA5WhcNMjgxMjI4MTA1nODA5WjB2MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEnBxMNU2FuIEZyYW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEfMB0GnA1UEAxMWdGxzY2Eub3JnMS5leGFtcGxlLmNvbTBZMBMGByqGSM49AgEGCCqGSM49nAwEHA0IABEbH7l3CiqLA4N4wgfilYgyEuxDrMAqDX6BrFOfWhymNosjh5FlJDHtNnGPDKhjtrI6e1q0NC0l6wh9h9TrBn7N2jXzBdMA4GA1UdDwEB/wQEAwIBpjAPBgNVnHSUECDAGBgRVHSUAMA8GA1UdEwEB/wQFMAMBAf8wKQYDVR0OBCIEIH7OaekSLJdanS0yuV9PCsuasGTt/+/35aVBXTVbII2rCMAoGCCqGSM49BAMCA0cAMEQCIEd+YP/6ntCzG/LueYTEio8ApQSyz94ju07pmc3LZJDKBAiALu66LKhOpKhogY9XEFg4TScOtn
el4dC6OnMMTmRsEtoA==n-----END CERTIFICATE-----n"


Anybody can help?
Thank you







regex bash sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 13:46







Julian Balling

















asked Dec 31 '18 at 11:20









Julian BallingJulian Balling

62




62













  • Prefix your code/data with four white spaces. Please take a look at editing-help.

    – Cyrus
    Dec 31 '18 at 11:22






  • 2





    I don't get you, n IS a newline.

    – Toto
    Dec 31 '18 at 11:22






  • 1





    sed doesn't permit the substitution string to straddle newlines without additional escaping. cat inserts the contents of a file, not the contents of a variable What exactly do you hope for your code to accomplish, and have you searched for similar questions already? This is a fairly common task.

    – tripleee
    Dec 31 '18 at 11:41











  • If the original CERT is in a file, sed /@INSERT_ORIGINAL_CERT@/{r certfile; d;}' file might work.

    – tripleee
    Dec 31 '18 at 12:01











  • That is not a valid key. What someone has done is "half-encoding" (I don't know a better term) the newlines - they have added the literal string "n" before every newline. What you very likely want is either the original key with no "n" strings or a single line string where every newline has been replaced with "n".

    – l0b0
    Dec 31 '18 at 19:47



















  • Prefix your code/data with four white spaces. Please take a look at editing-help.

    – Cyrus
    Dec 31 '18 at 11:22






  • 2





    I don't get you, n IS a newline.

    – Toto
    Dec 31 '18 at 11:22






  • 1





    sed doesn't permit the substitution string to straddle newlines without additional escaping. cat inserts the contents of a file, not the contents of a variable What exactly do you hope for your code to accomplish, and have you searched for similar questions already? This is a fairly common task.

    – tripleee
    Dec 31 '18 at 11:41











  • If the original CERT is in a file, sed /@INSERT_ORIGINAL_CERT@/{r certfile; d;}' file might work.

    – tripleee
    Dec 31 '18 at 12:01











  • That is not a valid key. What someone has done is "half-encoding" (I don't know a better term) the newlines - they have added the literal string "n" before every newline. What you very likely want is either the original key with no "n" strings or a single line string where every newline has been replaced with "n".

    – l0b0
    Dec 31 '18 at 19:47

















Prefix your code/data with four white spaces. Please take a look at editing-help.

– Cyrus
Dec 31 '18 at 11:22





Prefix your code/data with four white spaces. Please take a look at editing-help.

– Cyrus
Dec 31 '18 at 11:22




2




2





I don't get you, n IS a newline.

– Toto
Dec 31 '18 at 11:22





I don't get you, n IS a newline.

– Toto
Dec 31 '18 at 11:22




1




1





sed doesn't permit the substitution string to straddle newlines without additional escaping. cat inserts the contents of a file, not the contents of a variable What exactly do you hope for your code to accomplish, and have you searched for similar questions already? This is a fairly common task.

– tripleee
Dec 31 '18 at 11:41





sed doesn't permit the substitution string to straddle newlines without additional escaping. cat inserts the contents of a file, not the contents of a variable What exactly do you hope for your code to accomplish, and have you searched for similar questions already? This is a fairly common task.

– tripleee
Dec 31 '18 at 11:41













If the original CERT is in a file, sed /@INSERT_ORIGINAL_CERT@/{r certfile; d;}' file might work.

– tripleee
Dec 31 '18 at 12:01





If the original CERT is in a file, sed /@INSERT_ORIGINAL_CERT@/{r certfile; d;}' file might work.

– tripleee
Dec 31 '18 at 12:01













That is not a valid key. What someone has done is "half-encoding" (I don't know a better term) the newlines - they have added the literal string "n" before every newline. What you very likely want is either the original key with no "n" strings or a single line string where every newline has been replaced with "n".

– l0b0
Dec 31 '18 at 19:47





That is not a valid key. What someone has done is "half-encoding" (I don't know a better term) the newlines - they have added the literal string "n" before every newline. What you very likely want is either the original key with no "n" strings or a single line string where every newline has been replaced with "n".

– l0b0
Dec 31 '18 at 19:47












2 Answers
2






active

oldest

votes


















0














That is not a valid key. What someone has done is "half-encoding" (I don't know a better term) the newlines - they have added the literal string "n" before every newline. What you very likely want is either the original key with no "n" strings or a single line string where every newline has been replaced with "n".



With the original value you can use replace instead - it supports newlines in the replacement value:



$ replace foo $'foonbar' <<< $'xnfoony'
x
foo
bar
y


Your case should be simply replace 'INSERT_ORG1_CA_CERT' "$(< $replacementOrg1)" "$file" > "$org1".






share|improve this answer































    0














    The substitute command isn't very good with multi-line replacement strings. But we can use GNU sed's read command to work around that:



     echo "${replacementOrg1}" | 
    sed -e '/INSERT_ORG1_CA_CERT/{r /dev/stdin' -e ';d}' ${file} > ${org1}


    How it works:





    1. echo the multi-line string, piping it to /dev/stdin.

    2. When sed finds the target "INSERT_ORG1_CA_CERT" it reads /dev/stdin and outputs the contents

    3. then deletes the search string line, (which is presumed to contain no other text).


    The tricky part is the inadequately documented r command -- sed assumes everything after the r is part of the filename. If we tried '/INSERT_ORG1_CA_CERT/{r /dev/stdin;d}' it would bomb with the error:



    unmatched '{'



    Because sed would think the filename was literally "/dev/stdin;d}". But the error message doesn't complain about the missing file, because sed never complains about a missing r filename. Instead sed complains that there's no } closing brace, because sed thinks the } is part of the filename.



    To avoid that error we stick an ' -e ' in there.






    share|improve this answer





















    • 1





      The r command is poorly standardized. The semicolon variant I posted in a comment works fine on MacOS.

      – tripleee
      Jan 2 at 9:49













    • Link to tripleee's comment.

      – agc
      Jan 2 at 13:25













    • @tripleee, Thanks, that's interesting about MacOS (and presumably BSD sed). It's very surprising, (embarrassing even...), to see that your prior code is functionally the same as this answer, even though I hadn't read that earlier comment and code until now.

      – agc
      Jan 2 at 13:32













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986846%2fneed-help-using-sed-to-stop-interpreting-n-as-new-line-in-linux-bash-scripts%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    That is not a valid key. What someone has done is "half-encoding" (I don't know a better term) the newlines - they have added the literal string "n" before every newline. What you very likely want is either the original key with no "n" strings or a single line string where every newline has been replaced with "n".



    With the original value you can use replace instead - it supports newlines in the replacement value:



    $ replace foo $'foonbar' <<< $'xnfoony'
    x
    foo
    bar
    y


    Your case should be simply replace 'INSERT_ORG1_CA_CERT' "$(< $replacementOrg1)" "$file" > "$org1".






    share|improve this answer




























      0














      That is not a valid key. What someone has done is "half-encoding" (I don't know a better term) the newlines - they have added the literal string "n" before every newline. What you very likely want is either the original key with no "n" strings or a single line string where every newline has been replaced with "n".



      With the original value you can use replace instead - it supports newlines in the replacement value:



      $ replace foo $'foonbar' <<< $'xnfoony'
      x
      foo
      bar
      y


      Your case should be simply replace 'INSERT_ORG1_CA_CERT' "$(< $replacementOrg1)" "$file" > "$org1".






      share|improve this answer


























        0












        0








        0







        That is not a valid key. What someone has done is "half-encoding" (I don't know a better term) the newlines - they have added the literal string "n" before every newline. What you very likely want is either the original key with no "n" strings or a single line string where every newline has been replaced with "n".



        With the original value you can use replace instead - it supports newlines in the replacement value:



        $ replace foo $'foonbar' <<< $'xnfoony'
        x
        foo
        bar
        y


        Your case should be simply replace 'INSERT_ORG1_CA_CERT' "$(< $replacementOrg1)" "$file" > "$org1".






        share|improve this answer













        That is not a valid key. What someone has done is "half-encoding" (I don't know a better term) the newlines - they have added the literal string "n" before every newline. What you very likely want is either the original key with no "n" strings or a single line string where every newline has been replaced with "n".



        With the original value you can use replace instead - it supports newlines in the replacement value:



        $ replace foo $'foonbar' <<< $'xnfoony'
        x
        foo
        bar
        y


        Your case should be simply replace 'INSERT_ORG1_CA_CERT' "$(< $replacementOrg1)" "$file" > "$org1".







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 31 '18 at 19:51









        l0b0l0b0

        34k1585147




        34k1585147

























            0














            The substitute command isn't very good with multi-line replacement strings. But we can use GNU sed's read command to work around that:



             echo "${replacementOrg1}" | 
            sed -e '/INSERT_ORG1_CA_CERT/{r /dev/stdin' -e ';d}' ${file} > ${org1}


            How it works:





            1. echo the multi-line string, piping it to /dev/stdin.

            2. When sed finds the target "INSERT_ORG1_CA_CERT" it reads /dev/stdin and outputs the contents

            3. then deletes the search string line, (which is presumed to contain no other text).


            The tricky part is the inadequately documented r command -- sed assumes everything after the r is part of the filename. If we tried '/INSERT_ORG1_CA_CERT/{r /dev/stdin;d}' it would bomb with the error:



            unmatched '{'



            Because sed would think the filename was literally "/dev/stdin;d}". But the error message doesn't complain about the missing file, because sed never complains about a missing r filename. Instead sed complains that there's no } closing brace, because sed thinks the } is part of the filename.



            To avoid that error we stick an ' -e ' in there.






            share|improve this answer





















            • 1





              The r command is poorly standardized. The semicolon variant I posted in a comment works fine on MacOS.

              – tripleee
              Jan 2 at 9:49













            • Link to tripleee's comment.

              – agc
              Jan 2 at 13:25













            • @tripleee, Thanks, that's interesting about MacOS (and presumably BSD sed). It's very surprising, (embarrassing even...), to see that your prior code is functionally the same as this answer, even though I hadn't read that earlier comment and code until now.

              – agc
              Jan 2 at 13:32


















            0














            The substitute command isn't very good with multi-line replacement strings. But we can use GNU sed's read command to work around that:



             echo "${replacementOrg1}" | 
            sed -e '/INSERT_ORG1_CA_CERT/{r /dev/stdin' -e ';d}' ${file} > ${org1}


            How it works:





            1. echo the multi-line string, piping it to /dev/stdin.

            2. When sed finds the target "INSERT_ORG1_CA_CERT" it reads /dev/stdin and outputs the contents

            3. then deletes the search string line, (which is presumed to contain no other text).


            The tricky part is the inadequately documented r command -- sed assumes everything after the r is part of the filename. If we tried '/INSERT_ORG1_CA_CERT/{r /dev/stdin;d}' it would bomb with the error:



            unmatched '{'



            Because sed would think the filename was literally "/dev/stdin;d}". But the error message doesn't complain about the missing file, because sed never complains about a missing r filename. Instead sed complains that there's no } closing brace, because sed thinks the } is part of the filename.



            To avoid that error we stick an ' -e ' in there.






            share|improve this answer





















            • 1





              The r command is poorly standardized. The semicolon variant I posted in a comment works fine on MacOS.

              – tripleee
              Jan 2 at 9:49













            • Link to tripleee's comment.

              – agc
              Jan 2 at 13:25













            • @tripleee, Thanks, that's interesting about MacOS (and presumably BSD sed). It's very surprising, (embarrassing even...), to see that your prior code is functionally the same as this answer, even though I hadn't read that earlier comment and code until now.

              – agc
              Jan 2 at 13:32
















            0












            0








            0







            The substitute command isn't very good with multi-line replacement strings. But we can use GNU sed's read command to work around that:



             echo "${replacementOrg1}" | 
            sed -e '/INSERT_ORG1_CA_CERT/{r /dev/stdin' -e ';d}' ${file} > ${org1}


            How it works:





            1. echo the multi-line string, piping it to /dev/stdin.

            2. When sed finds the target "INSERT_ORG1_CA_CERT" it reads /dev/stdin and outputs the contents

            3. then deletes the search string line, (which is presumed to contain no other text).


            The tricky part is the inadequately documented r command -- sed assumes everything after the r is part of the filename. If we tried '/INSERT_ORG1_CA_CERT/{r /dev/stdin;d}' it would bomb with the error:



            unmatched '{'



            Because sed would think the filename was literally "/dev/stdin;d}". But the error message doesn't complain about the missing file, because sed never complains about a missing r filename. Instead sed complains that there's no } closing brace, because sed thinks the } is part of the filename.



            To avoid that error we stick an ' -e ' in there.






            share|improve this answer















            The substitute command isn't very good with multi-line replacement strings. But we can use GNU sed's read command to work around that:



             echo "${replacementOrg1}" | 
            sed -e '/INSERT_ORG1_CA_CERT/{r /dev/stdin' -e ';d}' ${file} > ${org1}


            How it works:





            1. echo the multi-line string, piping it to /dev/stdin.

            2. When sed finds the target "INSERT_ORG1_CA_CERT" it reads /dev/stdin and outputs the contents

            3. then deletes the search string line, (which is presumed to contain no other text).


            The tricky part is the inadequately documented r command -- sed assumes everything after the r is part of the filename. If we tried '/INSERT_ORG1_CA_CERT/{r /dev/stdin;d}' it would bomb with the error:



            unmatched '{'



            Because sed would think the filename was literally "/dev/stdin;d}". But the error message doesn't complain about the missing file, because sed never complains about a missing r filename. Instead sed complains that there's no } closing brace, because sed thinks the } is part of the filename.



            To avoid that error we stick an ' -e ' in there.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 2 at 13:36

























            answered Jan 2 at 2:56









            agcagc

            4,8571338




            4,8571338








            • 1





              The r command is poorly standardized. The semicolon variant I posted in a comment works fine on MacOS.

              – tripleee
              Jan 2 at 9:49













            • Link to tripleee's comment.

              – agc
              Jan 2 at 13:25













            • @tripleee, Thanks, that's interesting about MacOS (and presumably BSD sed). It's very surprising, (embarrassing even...), to see that your prior code is functionally the same as this answer, even though I hadn't read that earlier comment and code until now.

              – agc
              Jan 2 at 13:32
















            • 1





              The r command is poorly standardized. The semicolon variant I posted in a comment works fine on MacOS.

              – tripleee
              Jan 2 at 9:49













            • Link to tripleee's comment.

              – agc
              Jan 2 at 13:25













            • @tripleee, Thanks, that's interesting about MacOS (and presumably BSD sed). It's very surprising, (embarrassing even...), to see that your prior code is functionally the same as this answer, even though I hadn't read that earlier comment and code until now.

              – agc
              Jan 2 at 13:32










            1




            1





            The r command is poorly standardized. The semicolon variant I posted in a comment works fine on MacOS.

            – tripleee
            Jan 2 at 9:49







            The r command is poorly standardized. The semicolon variant I posted in a comment works fine on MacOS.

            – tripleee
            Jan 2 at 9:49















            Link to tripleee's comment.

            – agc
            Jan 2 at 13:25







            Link to tripleee's comment.

            – agc
            Jan 2 at 13:25















            @tripleee, Thanks, that's interesting about MacOS (and presumably BSD sed). It's very surprising, (embarrassing even...), to see that your prior code is functionally the same as this answer, even though I hadn't read that earlier comment and code until now.

            – agc
            Jan 2 at 13:32







            @tripleee, Thanks, that's interesting about MacOS (and presumably BSD sed). It's very surprising, (embarrassing even...), to see that your prior code is functionally the same as this answer, even though I hadn't read that earlier comment and code until now.

            – agc
            Jan 2 at 13:32




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986846%2fneed-help-using-sed-to-stop-interpreting-n-as-new-line-in-linux-bash-scripts%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas