Change specific ip address in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change specific ip address in a file
# 1  
Old 04-22-2010
Change specific ip address in a file

I need to change a line from a xen cfg file using sed if it's possible. The original line is:

Code:
vif         = [ 'ip=123.456.789.123,mac=00:16:3E:7D:16:BF,vifname=veth107', 'ip=10.1.10.4,vifname=veth107a' ]

I want to change ONLY the IP address of the second part ==> ip=10.1.10.4 to another IP --> ip=192.222.11.6 The first one ip=123.456.789.123 keeps untouchable.

My new line shoud be like this now:

Code:
vif         = [ 'ip=123.456.789.123,mac=00:16:3E:7D:16:BF,vifname=veth107', 'ip=192.222.11.6,vifname=veth107a' ]

Can you help me?

thanks in advance.
regards
Israel

Last edited by zaxxon; 04-22-2010 at 10:48 AM.. Reason: use code tags please, ty
# 2  
Old 04-22-2010
Try this if the ip address occurs only once in the file:
Code:
sed 's/ip=10.1.10.4/ip=192.222.11.6/' file

# 3  
Old 04-22-2010
Tools

Arrrr, this is a very very dirty hack:
Code:
$ cat myconf
vif         = [ 'ip=123.456.789.123,mac=00:16:3E:7D:16:BF,vifname=veth107', 'ip=10.1.10.4,vifname=veth107a' ]
$ 
$ cat myconf | sed ''/ip=10.1.10.4,v/s//ip=192.222.11.6,v/'' > myconf
$ 
$ cat myconf
vif         = [ 'ip=123.456.789.123,mac=00:16:3E:7D:16:BF,vifname=veth107', 'ip=192.222.11.6,vifname=veth107a' ]
$

# 4  
Old 04-22-2010
opss, sorry guys...

I forgot to say about the value I want to change... ip=10.1.10.4 is a random ip, I just want to change the original random value to other random value too...

I mean, change the second part of value ip=un.known.ip.add to ip=other.ip.add.ress

Am I asking too much? :-)

regards
Israel.
# 5  
Old 04-22-2010
i'd do it with perl:

Code:
perl -i -ne's/(ip=.+)(ip=.+?,)/$1ip=new.ip.address,/;print'



---------- Post updated at 02:13 PM ---------- Previous update was at 02:12 PM ----------

sorry forgot to include the filename at the end:

Code:
perl -i -ne's/(ip=.+)(ip=.+?,)/$1ip=new.ip.address,/;print' file


Last edited by Scott; 04-22-2010 at 04:33 PM.. Reason: Code tags...
# 6  
Old 04-22-2010
very nice rugdog:

What if I need to remove that new ip entry I already changed the way you said? I want this output now:


Code:
vif         = [ 'ip=123.456.789.123,mac=00:16:3E:7D:16:BF,vifname=veth107', 'vifname=veth107a' ]

really appreciate your help
regards
Israel.

Last edited by Scott; 04-23-2010 at 05:58 PM.. Reason: Code tags, please...
# 7  
Old 04-22-2010
that would be:

Code:
perl -i -ne's/(ip=.+)(ip=.+?,)/$1/;print' file

be aware that the "-i" option is the in-place edit option of perl, that means that the file is modified right away when you run the script.

Last edited by Scott; 04-23-2010 at 05:58 PM.. Reason: Code tags, please...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to change specific string to new value if found in text file

I am trying to use awk to change a specific string in a field, if it is found, to another value. In the tab-delimited file the text in bold in $3 contains the string 23, which is always right before a ., if it is present. I am trying to change that string to X, keeping the formatting and the... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. UNIX for Beginners Questions & Answers

Delete columns with a specific title XXX, where the position change in each file

Goodmorning, I know how to cut a string and a column, and how to find a word. I have a file with over 100 columns. All columns have a title in the first line. I have to delete all columns with the XXX title. I can't use cut -f because the position of XXX columns change in each file, and in... (14 Replies)
Discussion started by: echo manolis
14 Replies

3. Shell Programming and Scripting

To change Specific Lines in An XML file

hi Guys, this is my requirement, there is a huge xml file of this i have to change 3 lines with out opening the file /users/oracle > cat lnxdb-pts-454.xml|egrep "s_virtual|s_cluster|s_dlsnstatus" <cluster_port oa_var="s_clusterServicePort">9998</cluster_port> <host... (2 Replies)
Discussion started by: smarlaku
2 Replies

4. Shell Programming and Scripting

how to change specific value for a entry in the file

Hello All, can someone please suggest me a one line command to change a specific value that is associated to an entry in the file. for example #more schedulefile quartz.job.manual.bonus.schedule=0 0 9 ? * * # it should be changed to #more schedulefile... (5 Replies)
Discussion started by: bobby320
5 Replies

5. UNIX for Dummies Questions & Answers

How can I search and change an specific string in a file

Dear All, New to Linux/Unix OS, my Linux version is 2010 x86_64 x86_64 x86_64 GNU/Linux As titled, I wonder if you can help to provide a solution to find and change an specific string in a file The file include a lots of data in following configuration but might be various in... (3 Replies)
Discussion started by: axel
3 Replies

6. Shell Programming and Scripting

How to change a number on a specific lines in a file with shell?

Hello My problem is that I want to change some specific numbers in a file. It is like, 2009 10 3 2349 21.3 L 40.719 27.388 10.8 FRO 7 0.8 1.1LFRO 2.6CFRO 1.1LMAM1 GAP=157 1.69 5.7 5.9 5.8 0.5405E+01 0.4455E+00 0.1653E+02E STAT SP IPHASW D HRMM SECON CODA AMPLIT... (11 Replies)
Discussion started by: miriammiriam
11 Replies

7. Shell Programming and Scripting

SED 4.1.4 - INI File Change Problem in Variables= in Specific [Sections] (Guru Help)

GNU sed version 4.1.4 on Windows XP SP3 from GnuWin32 I think that I've come across a seemingly simple text file change problem on a INI formatted file that I can't do with SED without side effects edge cases biting me. I've tried to think of various ways of doing this elegantly and quickly... (5 Replies)
Discussion started by: JakFrost
5 Replies

8. UNIX for Dummies Questions & Answers

Script to change/find/delete/install a specific file

Hi Very much a newbie to UNIX & scripting, but have identified an area within work that would benefit from being automated, as its repeated manually very often, and it looks like the ideal first script! What I need to do is change directory to a users home (cd ~), and then find and remove a... (6 Replies)
Discussion started by: Great Uncle Kip
6 Replies

9. UNIX for Dummies Questions & Answers

Change Specific Line of a File

Hi everyone, I am attempting to do something that should be very simple. How do I replace a specific line of a file with different text, and then save that file to its original name? I believe I want to use the sed command with the c option, but I after trying many times, I can't get the right... (10 Replies)
Discussion started by: msb65
10 Replies

10. Shell Programming and Scripting

How to change a specific character in a file

Hi, I have a data file with following structure: a|b|c|d|3|f1|f2|f3 a|b|c|d|5|f1|f2|f3|f4|f5 I want to change this data to: a|b|c|d|3|f1;f2;f3 a|b|c|d|5|f1;f2;f3;f4;f5 Data in column 5 tells the number of following fields. All fields delimiter after the 5th column needs to be... (6 Replies)
Discussion started by: sdubey
6 Replies
Login or Register to Ask a Question