在文件中搜索单词然后替换其他文件中的文本

Search for word in file then replace text in other file

我有两个文件。 文件 1 包含如下用户名和密码:

[reader]
label                         = anylabel
protocol                      = cccam
device                        = some.url,13377
user                          = Username1
password                      = password1
password2                     = password2
inactivitytimeout             = 30
group                         = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
cccversion                    = 2.3.2
ccckeepalive                  = 1

File2 包含如下一行:

http://link:port/username/password/12345

现在我有了这个“代码”来更改 File2 中的 Username/Password:

UsernameOLD=Username1
PasswordOLD=password1
UsernameNEW=Username2
PasswordNEW=password2

sed -i -e "s/\/$UsernameOLD\/$PasswordOLD/\/$UsernameNEW\/$PasswordNEW/" /etc/enigma2/file2.cfg

现在我有不同的用户名,它们在 File1 中始终是最新的。我现在正在寻找一种解决方案,将 File1 中的用户名和密码 2 写入变量,然后在 File2 中设置新的用户名和密码.

所以作为菜鸟,伪代码应该是这样的:

find "username" & "password1" in file1 
set "username" as $UsernameNEW and
    "password1" as $PasswordNEW and
then just execute my sed command.

有人可以帮忙吗?我想我可以为此使用 grep 吗?但老实说,我很高兴我得到了这个带有变量的 sed 命令:D

这里有一些可以帮助您入门的内容。

oscam.conf

[reader]
label                         = anylabel
protocol                      = cccam
device                        = some.url,13377
user                          = xxx1
password                      = password1
inactivitytimeout             = 30
group                         = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
cccversion                    = 2.3.2
ccckeepalive                  = 1


[reader]
label                         = anylabel
protocol                      = cccam
device                        = test.url,13377
user                          = yyy1
password                      = password1
inactivitytimeout             = 30
group                         = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
cccversion                    = 2.3.2
ccckeepalive                  = 1

密码文件的格式我改了一些,但用原来的格式也可以。

密码(格式为旧用户、新用户、旧密码、新用户)

xxx1,xxx2,passxxx1,passxxx2
yyy1,yyy2,passyyy1,passyyy2

awk 命令

awk -F, 'FNR==NR {usr[]=;pass[]=;next} FNR!=NR{FS=" = "}  /^user/ {t=;="= "usr[]} /^password/ {="= "pass[t]} 1' passwd oscam.reader

结果

[reader]
label                         = anylabel
protocol                      = cccam
device                        = some.url,13377
user                          = xxx2
password                      = passxxx2
inactivitytimeout             = 30
group                         = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
cccversion                    = 2.3.2
ccckeepalive                  = 1


[reader]
label                         = anylabel
protocol                      = cccam
device                        = test.url,13377
user                          = yyy2
password                      = passyyy2
inactivitytimeout             = 30
group                         = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
cccversion                    = 2.3.2
ccckeepalive                  = 1

快速而肮脏——将旧密码加载到环境参数中:

set -- $(grep -m1 -A1 '^user' File1)
sed -i -e "s#/${UsernameOLD}/${PasswordOLD}#//#;T;q" /etc/enigma2/file2.cfg

工作原理:grep 输出六个 space 分隔项,set 变成命令行参数 ... 。我们只需要 </code> 和 <code>.