-3

I am creating a script that I can run and it will simply ask me the common location name...i.e SEC-DF1 and it will fetch the ip of that site from within script. My problem is taking that IP and replacing

right=IP_ADDRESS

with

right=NEW_IP_ADDRESS

I need this so I can call the script as I will be changing the value of right so often for testing.

I have been messing with sed until someone mentioned awk...this stuff has such horrid documentation I keep getting all types errors or weird results on the test file I am messing with.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Dan
  • 185
  • 2
  • 2
  • 12
  • If I understand correctly, you want to write a script that modifies another script. Is this correct, or do I misunderstand? – thb Jun 25 '12 at 17:19
  • I want to place this command into a script to be run and modify a ipsec.conf file. – Dan Jun 25 '12 at 17:24
  • What have you tried? Please [edit your question](http://stackoverflow.com/posts/11194050/edit) to show before-and-after samples, and explain what you want to achieve with more detail. – ghoti Jun 26 '12 at 03:31

2 Answers2

1

Since this is a straight forward substitution, I would just use sed:

sed -e 's/^right=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/right=192.168.1.92/' filename

This will match right= at the beginning of a line followed by an IP address and replace it with the IP of your choosing.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
0

This command will modify your script:

NEW_IP_ADDRESS=101.102.103.104 sed -i "s/^(right=).*$/\\1$NEW_IP_ADDRESS/" script
thb
  • 13,796
  • 3
  • 40
  • 68
  • This is equivalent to `sed 's/^(right=).*$/\1/' script`. The assignment of `NEW_IP_ADDRESS` does not modify the argument passed to sed. – William Pursell Jun 25 '12 at 20:10