Reputation: 486
Context:
I have several .properties
files that contain IP addresses within them of the form:
advertised.listeners=PLAINTEXT://ip-68-51-0-82.ec2.internal:9092
advertised.host.name=ip-68-51-0-82.ec2.internal
(The IP addresses have been randomized for anonymity)
Solution in Theory: I am trying to automatically replace the mentioned IP addresses within the .properties
file with the result of the bash command hostname -i
from within a bash script (as opposed to manually editing the IP's in VIM).
Problem: hostname -i
returns the IP address I need, but in a different format. It returns 62.57.0.85
which is in dot notation rather than dash notation.
Goal: To change the text of the file to now read:
advertised.listeners=PLAINTEXT://ip-62-57-0-85.ec2.internal:9092
advertised.host.name=ip-62-57-0-85.ec2.internal
How would I do this? Would I use something like sed
?
UPDATE (7/6/2021 at 19:06): After looking at the answer and identifying further constraints on the problem, here is the code that worked for me:
sed -i "s|68-51-0-82|$(hostname -i | tr . -)|g" server.properties
To further elaborate, I am able to hardcode the IP I wish to replace because I know that it will always be the same. If you don't, and need to pattern match against any potential IP address, refer to the original answer to this question.
As for the usage of tr
, it's a quick and easy way to convert the format of the hostname (which is in dot notation by default) to dash notation. I then use the output of the tr
command directly in the sed
command via a $
variable
Upvotes: 0
Views: 184
Reputation: 33819
UPDATED based on further comments/clarifications by OP:
$ oldip='68-51-0-82' # OP knows in advance the IP that needs to be replaced in the file
$ newip='62-57-0-85' # OP has already swapped out periods for hyphens
$ sed "s/${oldip}/${newip}/g" .properties
advertised.listeners=PLAINTEXT://ip-62-57-0-85.ec2.internal:9092
advertised.host.name=ip-62-57-0-85.ec2.internal
If OP knows oldip
will ALWAYS be the same (ie, 68-51-0-82
) then this could be hardcoded into the sed
call like such:
$ sed "s/68-51-0-82/${newip}/g" .properties
advertised.listeners=PLAINTEXT://ip-62-57-0-85.ec2.internal:9092
advertised.host.name=ip-62-57-0-85.ec2.internal
Assumptions:
hostname -i
==> fe80::5454:7ccf:aa17:f765%13 10.8.1.6 192.168.1.4
... so I'm assuming OP has already parsed the new ip address into a variable (eg, newip
)<stuff>.ip-n1-n2-n3-n4.<stuff>
)One idea using sed
:
$ cat .properties
advertised.listeners=PLAINTEXT://ip-68-51-0-82.ec2.internal:9092
advertised.host.name=ip-68-51-0-82.ec2.internal
$ newip='62.57.0.85'
$ sed "s/ip-[^\.]+\./ip-${newip//./-}\./" .properties
advertised.listeners=PLAINTEXT://ip-62-57-0-85.ec2.internal:9092
advertised.host.name=ip-62-57-0-85.ec2.internal
Where:
ip-[^\.]+\.
- match the string ip-
+ <everything that's not a period>
+ a literal period (.
)ip-${newip//./-}\.
- replace with string ip-
+ our variable (periods replaced with hyphens) + a literal period (.
)If OP is looking to overwrite .properties
, and has verified this sed
command does what's expected, the -i
flag can be added to tell sed
to overwrite the input file (ie, .properties
).
Upvotes: 3