1

I have figured out and tried replacing strings with the help of gsed -i command like this:

gsed -i 's/sdkUniqueKey=""/sdkUniqueKey="123"/g' AppConstants.txt

Now I want to do the same operation on another string in my file but as my question states, I need to copy the contents from a different file first and then replace a string, something like:

gsed -i 's/sdkPrivateKey=""/sdkPrivateKey="contentsCopiedFromAnotherFile"/g' AppConstants.txt

One more thing, the contents (to be copied), have next line and white space in it, which I would like to remove before copying. Also it has backslash and forward slashes, hope they don't create any issues while replacing the content). Here is what I am trying to copy:

-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDNGillPEfz8d7W
0fyJejF9AYeo8OowcdOcxrpzs4IiXCwPEP1MOHAaOwGTdMwSAeQjw9WOYpE1q+DU
I+Zhh4DVUR8dIdYQtXe+oK/QfhVQMJ3AjTKRvhUmFciGwxXlnLBIkN/ePplNdq9Z
Y5DrSR0lE8X2dD+ZRAkQRpsY8TE48b9f443sbsU4sMvNaxd2XTxe2TLYRvB00w6Q
3lqZiKLzttINBCPoCjhJwBdhcF/LHsCmYhfElPqJxH27BTGBOnbICdmazdnChXQg
3hhsbJmnNDe17Spw0lY
-----END PRIVATE KEY-----

I am able to copy the contents of a file into a variable as well:

contents ="`cat fileToBeRead`"

All I need is to remove white spaces and new lines from this string and use this "contents" variable in my gsed command

10
  • echo $var > $fileToBeRead is strange, it redirects the value of a variable var into file named $fileToBeRead. Commented Jun 5, 2019 at 11:29
  • @KamilCuk I am fairly new to bash and linux, I might have understood wrong. Commented Jun 5, 2019 at 11:31
  • 1
    you can store the contents you want to replace into a variable, eg. var=$(grep ...) and substitute it into your sed command Commented Jun 5, 2019 at 11:42
  • Do you want to replace the contents of the $fileToBeRead with the new ones or you want to append them instead? Commented Jun 5, 2019 at 12:05
  • I want to copy the contents of "fileToBeRead" and substitute a string in another file with this content. Commented Jun 5, 2019 at 12:07

1 Answer 1

3

If I have properly understood your question, the following should do the trick:

#!/bin/bash

fileToBeRead="key.txt" #Whatever

var=$(tr -d '[:blank:]\n' < $fileToBeRead)
sed -i "s#sdkPrivateKey=\"\"#sdkUniqueKey=\"$var\"#g" AppConstants.txt

Since the key contains backslashes/slashes you should use a different separator for sed (e.g. #) or the sentence will be misparsed.

EDIT:

KEY="$var" perl -pi -e 's/sdkPrivateKey=""/sdkUniqueKey="$ENV{KEY}"/g' AppConstants.txt

perl can be used instead of sed in order to avoid sed's separator issues. Check out @DennisWilliamson's comment below.

Sign up to request clarification or add additional context in comments.

6 Comments

But this assumes that the key doesn't contain pound signs (or any other character you might choose as an alternative sed delimiter).
That's right, but no the problem here IMO. Choosing a good or bad separator (according to the data to be processed) is up to the OP. May be he can use a non-printable separator (I don't know 100% if this would work in this situation)
I am getting "-bash: “p.pem”: No such file or directory", for fileToBeRead=“p.pem” . While when I do ls, I can see that file.
@nr5 Both the script and the keyfile are in the same dir? Are you sure of being using the real filename?
I just removed the double quotes and it worked: fileToBeRead=key.txt
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.