cancel
Showing results for 
Search instead for 
Did you mean: 

sed with read input

 
johnho
Occasional Visitor

sed with read input

problem with shell scripting:
The sed command doesn't work.. Do you know what is the correct syntax of sed command?
I want to change the IP-address of the APN.

Many thanks.

John

#!/bin/sh

printf "Which APN you want to migrate? "
read APN
printf "\n\n"
printf "You want to migrate this APN: $APN"
printf "\n\n"
printf "Current IP of this APN: $APN"
printf "\n\n"
cat gprs.zone | grep "$APN"
printf "\n\n"
printf "\n\n"
sed '/$APN/s/10.10.10.12/10.10.10.225/g' gprs.zone > gprs.zone.new
sed '/$APN/s/10.10.10.40/10.10.10.229/g' gprs.zone > gprs.zone.new
printf "result"
printf "\n\n"
cat gprs.zone.new | grep "$APN"
exit

Printout of the shell script.
***********************************************

Which APN you want to migrate? test.com

You want to migrate this APN: test.com

Current IP of this APN: test.com

test.com IN A 10.10.10.40
test.com IN A 10.10.10.12


result

test.com IN A 10.10.10.40
test.com IN A 10.10.10.12

***********************************************
7 REPLIES
Steven E. Protter
Exalted Contributor

Re: sed with read input

Shalom,

Sed examples.

$commandline = "sed s/juf.org/juf.net/g ${filetoback} > $filetoback.bck";


system("${commandline}");
$commandline = "cp ${filetoback}.bck ${filetoback}";
system("${commandline}");

This is pulled from a perl script that goes to the command shell to do some sed conversion.

Pretend the perl part isn't around.

the cp command copies the output of the sed command over the original data file.

SEP

Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Mel Burslan
Honored Contributor

Re: sed with read input

have you tried:

cat gprs.zone | sed '/$APN/s/10.10.10.12/10.10.10.225/g' > gprs.zone.new

yet ?
________________________________
UNIX because I majored in cryptology...
James R. Ferguson
Acclaimed Contributor

Re: sed with read input

Hi:

Using:

# sed '/$APN/s/10.10.10.40/10.10.10.229/g'

...with the single quotes is not going to allow interpolation of your APN variable. Instead use:

# sed "/$APN/s/10.10.10.40/10.10.10.229/g"

Regards!

...JRF...
johnho
Occasional Visitor

Re: sed with read input

#!/bin/sh

printf "Which APN you want to migrate? "
read APN
printf "\n\n"
printf "You want to migrate this APN: $APN"
printf "\n\n"
printf "Current IP of this APN: $APN"
printf "\n\n"
cat gprs.zone | grep "$APN"
printf "\n\n"
printf "\n\n"

sed "/$APN/s/10.10.10.12/10.10.10.225/g" gprs.zone > gprs.zone.new
sed "/$APN/s/10.10.10.40/10.10.10.229/g" gprs.zone > gprs.zone.new


printf "result"
printf "\n\n"
cat gprs.zone.new | grep "$APN"
exit



Result of the script:


Which APN you want to migrate? test.com

You want to migrate this APN: test.com

Current IP of this APN: test.com

test.com IN A 10.10.10.40
test.com IN A 10.10.10.12


result

test.com IN A 10.10.10.229
test.com IN A 10.10.10.12

It is better now but still not 100%.
Problem now is: the second IP-address is not changed... do I need to do for or while loop?

Best regards,

John
James R. Ferguson
Acclaimed Contributor

Re: sed with read input

Hi (again) John:

> Problem now is: the second IP-address is not changed... do I need to do for or while loop?

Yes, you overwrote your output when you did the second substitution!

# sed -e "/$APN/s/10.10.10.12/10.10.10.225/g" -e "/$APN/s/10.10.10.40/10.10.10.229/g" gprs.zone > gprs.zone.new

...will fix your problem.

Regards!

...JRF...
Tingli
Esteemed Contributor

Re: sed with read input

The funny thing is that sed won't work for the last line in a file, as the last line doesn't have a carriage return in its end. (such as \n or \r)

Re: sed with read input

>sed '/$APN/s/10.10.10.12/10.10.10.225/g'

To be pedantic, you should quote each ".":
sed /$APN/s/'10\.10\.10\.12/10.10.10.225/g'

>Tingli: The funny thing is that sed won't work for the last line in a file, as the last line doesn't have a newline in its end.

That's way it is suppose to work. sed only works on text files and lines must be terminated by a newline.
http://docs.hp.com/en/B2355-60130/glossary.9.html#d0e1257203