Sunday, July 10, 2022

[SOLVED] Search for word in file then replace text in other file

Issue

I have two files. File1 contains a Username and Password like this:

[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

and File2 contains a line like:

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

Now I have this "code" to change the Username/Password in File2:

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

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

Now I have different Usernames which are always up to date in File1. I'm now searching for a solution to write the Username and the Password2 from File1 to a variable and then set this new Username and Password in File2.

So as a noob the psuedocode should be something like:

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

Can anyone assist ? I guess I could use grep for this? But to be honest I'm happy I got this sed command with variables to work :D


Solution

Here is some to get you started.

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

For the password file I have changed the format some, but can be done with original format.

passwd (format oldUser,newUser,oldPass,newUser)

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

Awk command

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

result

[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


Answered By - Jotne
Answer Checked By - Timothy Miller (WPSolving Admin)