[英]How to read a file and replace a string in another file using shell script in linux?
I am try to read the value of the key from the text file.我试图从文本文件中读取密钥的值。 After reading it I wanted to concatenate this value with some string and replace that line in another file.
读完之后,我想将这个值与一些字符串连接起来,并在另一个文件中替换该行。 Please find the below code
请找到以下代码
source.txt file
USERNAME='cffde308-a1f8-436f-9750-619856e70b88'
PASSWORD='XEAstplhfKNAjUg2Z2oXjIe7o9D4VW3q2dtnkoiMEP0='
target.js file目标.js 文件
auth: {
username: 'zfgde308-a1f8-436f-9750-619856e70b89',
password: 'ZEAstplhfKNAjUg2Z2oXjIe7o9D4VW3q2dtnkoiMEP0=',
},
I wanted to copy username and password from the source file and replace it in the config file.我想从源文件中复制用户名和密码并在配置文件中替换它。 I tried the below code
我尝试了下面的代码
#! /bin/sh
source "/opt/source/source.txt"
USER="username: '$USERNAME',"
PWD="password: '$PASSWORD',"
echo $USER
echo $PWD
sed '/username/c\$USER' target.js
sed '/password/c\$PWD' target.js
And the result that i get it is我得到的结果是
[l21m23 source]$ ./ClientDetailsSetup.sh
',ername: 'cffde308-a1f8-436f-9750-619856e70b88
password: 'XEAstplhfKNAjUg2Z2oXjIe7o9D4VW3q2dtnkoiMEP0=',
auth: {
username: 'zfgde308-a1f8-436f-9750-619856e70b89',
$PWD
},
If some help at the earliest to resolve this issue would be of great help如果尽早解决这个问题会有很大帮助
#!/bin/sh
source "/opt/source/source.txt"
USERN="username: '$USERNAME',"
PWD="password: '$PASSWORD',"
echo $USERN
echo $PWD
sed -i "s/username:.*/$USERN/" target.js
sed -i "s/password:.*/$PWD/" target.js
There were a few issues:有几个问题:
USER
is a reserved shell variable so you have to give it a different name USER
是一个保留的 shell 变量,所以你必须给它一个不同的名字s/.../.../
to substitute and you also need to match the rest of the line in your regexp so the .*
is needed or the old credentials are still in the outputs/.../.../
替换,并且您还需要匹配正则表达式中该行的 rest ,因此需要.*
或旧凭据仍在 Z78E6221F6393D1356DZF681DB393D1356DZF681DB393D1356DZ814CECE-i
option on sed is needed for an in-place edit-i
选项'
, so changed to "
here'
分隔的字符串中执行,因此在此处更改为"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.