Replace a string in a file with contents copied from another file

前端 未结 1 980
清歌不尽
清歌不尽 2020-12-20 06:38

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

gsed -i \'s/sdkUniqueKey=\"\"/sdkUniqueKey=\"123\"/g\         


        
相关标签:
1条回答
  • 2020-12-20 07:03

    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.

    0 讨论(0)
提交回复
热议问题