I want to replace 2 same words in file(app.properties) with 2 different words using sed command.
Example:
mysql.host=<<CHANGE_ME>>
mysql.username=testuser
mysql.port=3306
mysql.db.password=<<CHANGE_ME>>
required output will be
mysql.host=localhost
mysql.username=testuser
mysql.port=3306
mysql.db.password=password123
I tried below command:
sed -e "s/<<CHANGE_ME>>/localhost/1" -e "s/<<CHANGE_ME>>/password123/2" app.properties > /home/centos/SCRIPT/io.properties_new
However I am getting localhost
at both the places.
4
Answers
This may be possible but I don’t know if this is really readable for everyone.
Something like this might suite you :
If you have any idea to improve this, don’t hesitate. I would really like to learn the best way to do this too 😀
I’m sure it’s not impossible, but also that you will not be able to figure out how it works once you find an answer. A better solution is to switch to a language which is more human-readable, so you can understand what it does.
The
BEGIN
block creates an arrayitems
of replacements. The main script then incrementsi
every time we perform a replacement, indexing further intoitems
for the replacement string.Perhaps this might help:
With sed, using a wonderfully confusing
if (first time) do x, else do y
logic:Writing each command of the sed script on its own line makes it more understandable, or at least easier for me to expain it:
Here’s the explanation:
/<CHANGE_ME>/{…}
means that the stuff in{…}
is only applied to lines matching<CHANGE_ME>
;bb
: "b
ranch to (go to):b
", in this case used to skip the first substitution command;:a
: a target for anotherb
ranch ort
est-and-branch command;s/…/…/
: you know what it does, but we skip this the first time the script is run;b
: branches to the end of the script, skipping everything (because we are giving no argument tob
);:b
: the target of the commandbb
at 1.;x
: swap patter space (the line you’re dealing with at the moment), with the hold space (a kind of variable that you can put stuff into viax
,h
, andH
commands);s/E//
: tries to match and delete aE
(just because that’s the initial of my name), which fails the first time we run this, because the hold space that we’ve swapped earlier with the patter space was empty;x
: undos what the previousx
did, so we’re back on working with the line matching<CHANGE_ME>
;ta
: tests if last peformeds/…/…/
command succeeded and, if so, it goes to:a
, otherwise it’s a no-op; the first time we run the script this is a no-op, because step 6 failed;s/…/…/
: you know what it does;x
: see aboves/^/E/
: inserts theE
at the beginning of the line, so that next time we run the script substitution of step 7 succeedes, step 9 successfully branches to:a
, step 3 is peformed for the first time, and step 4 exits the script for ever;x
: see above