0

I have a property file with an entry such as this:

some.url={ipaddress}/blah

I want to substitute the {ipaddress} with the ip of machine its running on.

I found this command:

ipconfig getifaddr en0

which outputs the ip.

I also found sed is useful for this sort of thing but I can't quite put it all together.

sed -i '' 's/{ipaddress}/192.168.0.1/g' test.properties

Where the hard-coded ip address 192.168.0.1 needs to be substituted for the machine's one.

Can anyone help?

5
  • use double quotes instead of single quotes for variable expansion. Commented May 27, 2014 at 16:26
  • i need more information such as how to capture the output of the ipconfig command to a variable to then use in the sed command. Commented May 27, 2014 at 16:28
  • Seems like you need to learn the basics of shell scripting, then. Capturing the output of a command is one of the first things you'll learn. Commented May 27, 2014 at 16:29
  • 1
    The g at the end of this sed expression will cause all instances of "{ipaddress}" to be replaced. Make sure that is what you want. Or do you just want to replace the instance on lines that begin with "some.url="? Commented May 27, 2014 at 16:31
  • all occurrences is fine Commented May 27, 2014 at 16:33

2 Answers 2

2

You can store the result of the ipconfig command in a variable like this:

ip=$(ipconfig getifaddr en0)

Then interpolate it into your sed command by surrounding the expression with double-quotes:

sed -i "" "s/{ipaddress}/$ip/g" test.properties
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

cat file
some.url=10.10.10.5/blah

awk -F"=|/" '{print $1 "=" ip "/" $3}' ip=$(ip route get 8.8.8.8 | awk '{print $NF;exit}') file
some.url=192.168.1.30/blah

ip route get 8.8.8.8 | awk '{print $NF;exit}' is one of the best way I have found to get correct IP. It works even on VPS servers and on servers with multiple IP

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.