Simple SED issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple SED issue
# 1  
Old 10-06-2010
Simple SED issue

Hi! I'm a newbie and can't find the exact sed to make this work. I've installed CentOS and am trying to pass variables to a network-config file.

variables:
$ipAddress
$netmask
$gateway

file:
DeviceList.Ethernet.eth0.IP=192.168.1.10
DeviceList.Ethernet.eth0.Netmask=255.255.255.0
DeviceList.Ethernet.eth0.Gateway=192.168.1.1

I'm looking to replace the eth0.IP=*.*.*.* with eth0.IP=$ipAddress, and so on for netmask and gateway. How could I get this to work?
# 2  
Old 10-06-2010
Code:
$
$
$ ipAddress="Athos"
$ netmask="Porthos"
$ gateway="Aramis"
$
$ cat f31
DeviceList.Ethernet.eth0.IP=192.168.1.10
DeviceList.Ethernet.eth0.Netmask=255.255.255.0
DeviceList.Ethernet.eth0.Gateway=192.168.1.1
$
$
$ sed -e "s/^\(.*IP=\).*$/\1$ipAddress/" -e "s/^\(.*Netmask=\).*$/\1$netmask/" -e "s/^\(.*Gateway=\).*$/\1$gateway/" f31
DeviceList.Ethernet.eth0.IP=Athos
DeviceList.Ethernet.eth0.Netmask=Porthos
DeviceList.Ethernet.eth0.Gateway=Aramis
$
$

tyler_durden
# 3  
Old 10-06-2010
Slightly different sed:
Code:
sed "/Netmask/s/=.*/=$netmask/;/IP=/s/=.*/=$ipAddress/;/Gateway/s/=.*/=$gateway/" infile

# 4  
Old 10-06-2010
Thanks guys for the prompt replies! My fault, I didn't provide all the background, so I did a little bit more research and testing and I got it to work so far .. *fingers crossed*.

I'm passing the sed command from a client browser page in a .php script. It didn't like the "" so I edited the code you provided and went to a single '

sudo sed -i 's/^\(.*IP=\).*$/\'1$ipAddress'/' infile;

I haven't put it all together yet but this part seems to work correctly.

After I'm done I'll take a whack at it with Scrutinizers code as well so I can help myself understand what I'm doing. Smilie
# 5  
Old 10-06-2010
Try this
Code:
sudo sed -i 's/^\(.*IP=\).*$/\1'$ipAddress'/' infile

Move 1 to before single quote
# 6  
Old 10-06-2010
By awk
Code:
awk '
BEGIN{FS=OFS="="}
/IP/ {$2="'$ipAddress'"}
/Netmask/ {$2="'$netmask'"}
/Gateway/ {$2="'$gateway'"}1
' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Simple sed command not working; could be a Mac/Linux vs. PC/Linux issue

Hello, I am on a Mac and trying to clean up some monthly files with a very simple SED: sed '3,10d;/<ACROSS>/,$d' input.txt > output.txt (from the input, delete lines 3 - 10; then delete from the line containing <ACROSS> to the end of the file) then output to output.txt Even when I try... (2 Replies)
Discussion started by: verbatim
2 Replies

2. Shell Programming and Scripting

Simple date issue

Hi , Here is the smaller version of the problem. Working individually as command ************************>echo $SHELL /bin/bash ************************>TO_DAY=`date` ************************>echo $TO_DAY Tue Jul 16 02:28:31 EDT 2013 ************************> Not working when... (5 Replies)
Discussion started by: Anupam_Halder
5 Replies

3. Shell Programming and Scripting

Simple issue, what is wrong?

I had this working a few days ago but I since changed it. Heres the code x=1 while 1 2 3 4 5 6 1=$(ps -ef | grep process | awk '{ print $2}') if then echo "The database is accepting connections..." echo "Now I will check the next process" 2=$(ps -ef | grep process1 |... (10 Replies)
Discussion started by: jeffs42885
10 Replies

4. Shell Programming and Scripting

Simple Cut issue

I have a long string that looks something like this.... <string>http://abc.com/40/20/zzz061-3472/dP3rXLpPPV2KC846nJ4VXpH7jt4b3LJgkL/tarfile_date.tar</string> I need to but the tar file name. So I need to put between "/" and ".tar</string>". My desired result should be "tarfile_date". (7 Replies)
Discussion started by: elbombillo
7 Replies

5. Shell Programming and Scripting

simple for loop/cat issue

ok.. so problem is: I have a file that reads: cat 123 1 and 2 3 and 4 5 and 6 I was using for loops to run through this information. Code: for i in `cat 123` do echo $i done shouldn't the output come as 1 and 2 (3 Replies)
Discussion started by: foal_11
3 Replies

6. Shell Programming and Scripting

simple if/then issue in korn shell

When I run this, it tests to Y, but why? If I take the double quotes off of test, i get the same. # !/bin/ksh TEST="N" if ; then echo $TEST" yes" else echo "no" fi (4 Replies)
Discussion started by: guessingo
4 Replies

7. Shell Programming and Scripting

Simple date issue

Hi all, i have used the search already before someone shouts at me and i have seen the 'datecalc' program but this is not working correctly for me in the shell and environment i am using. I am using solaris 10 and bourne shell. I have two dates '07-04-2009' and '05-05-2009'. I just need to... (2 Replies)
Discussion started by: muay_tb
2 Replies

8. UNIX for Advanced & Expert Users

simple issue..

I have a program. Everytime that I run that program ,it prints a disclaimer msg and prompts the user to accept the agreement (i.e: type 'y' or 'n'). Now I want to run the program multiple times with different inputs. So I wrote a script which runs in a loop and calls the program multiple times.... (2 Replies)
Discussion started by: the_learner
2 Replies

9. Programming

simple scanf issue ?

Hello everyone, I hope someone is awake to help me on this.. hey How can I do something like this: The user is asked is asked to enter an int value, but I want to provide a default value on stdout, which they can back space and change it to whatever they want.. for e.g: Enter the... (4 Replies)
Discussion started by: the_learner
4 Replies

10. UNIX for Dummies Questions & Answers

something simple, but i have no idea: a login issue

Hi all, I have a problem not really dramatic but realyl annoying: i've got a groups of users who logon a sunos 2.3 box via a windows telnet client (KEAI, but this is not the problem). they login as perso1 and password. It takes a very very very long time to get in. If i log on the same box... (2 Replies)
Discussion started by: penguin-friend
2 Replies
Login or Register to Ask a Question