Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using sed with a value from another command 1

Status
Not open for further replies.

millap

IS-IT--Management
Jun 13, 2001
70
GB
Hi all,

I'm looking for a way to pass sed a variable to change an IP address after a user has been asked for information on what IP address they'd like as part of a machine setup script.

I've already saved the relevant ip, subnet, route and gw information into seperate files which I'd like to pass back to sed to insert into the bsd rc.conf file to replace whats there already. The IP information was input using dialog. For example

dialog --backtitle "Network Information" --inputbox "Enter the units IP Address" 10 27 2>/tmp/config1

What I'd like to do is take the contents of config1 and pass it to sed for insertion into rc.conf. Is this possible?

Cheers
millap
 
Sure.

MYCONFIG=`cat /tmp/config1`

But why do that?

MYCONFIG=`dialog --backtitle "Network Information" --inputbox "Enter the units IP Address" 10 27 2>&1`

puts the result in $MYCONFIG immediately.

Tony Lawrence
Linux/Unix/Mac OS X Resources
 
Hi pcunix,

Thanks for the reply. I understand that statement but how do I pass the contents of $MYCONFIG to sed? It will replace a known IP address (10.2.3.4 for example).

I need to do a sed 's/10.2.3.4/<$MYCONFIG>/' is this possible?

Thanks
millap
 
sed "s/10\.2\.3\.4/$MYCONFIG/"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Awesome, thanks PHV! Works like a dream :)

Sed is complicated though :p

Thanks to both of you for your replies

millap
 
Hi,

What OS you are using ?
Do the line you want to change in your rc.conf looks like this ?

ifconfig_ed1="inet 192.168.0.89 netmask 255.255.255.0"

Ali
 
Sed is complicated though
It's not a sed issue but a shell quoting mechanism you have to understand better.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Yes aau, it's rc.conf

Cheers
millap
 
Sorry Ali(aau) should have said it's BSD 2.0 also...

Thanks
millap
 
Hi,

If the line to change is really like this with "spaces" as field separator:
ifconfig_ed1="inet 192.168.0.89 netmask 255.255.255.0"
$1 $2 $3 $4

$1 $2 $3 and $4 are fields for awk command with space as field separator

1st : get new_ip from user
2nd : get OLD IP from rc.conf
3d : substitute the new ip to the old one
last;: verif the resulted file and copy it to the old rc.conf

#begin script -----------------
# get the new IP from the user
NEW_IP=something you have
# scan /etc/rc.conf to get the old IP
OLD_IP=$(cat /etc/rc.conf |awk '/ifconfig_ed1/{print $2}')
# substitute the new IP to the old IP in /etc/rc.conf and write the resulted file in /tmp
cat /etc/rc.conf |awk -v ip=$NEW_IP '/ifconfig_ed1/{print $1,ip,$3,$4 }' > /tmp/rc.conf

# end script -------------

# the modified file is /tmp/rc.conf
# verify that this script does the job.

Ali


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top