Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I used bash sed to replace ip address in a group of files, i.e 10.1.1.x => 10.2.2.x-19:

file1: =>, 
file2: =>, 
file3: =>, 


I almost managed with:

for file in ifcfg* ; do sed -e "s/10.1.1./10.2.2./" $file >tmp/$file ; done

but couldn't figure out how to calculate and replace the last address position (x => x-19)

it's probably really easy...

share|improve this question
up vote 1 down vote accepted

Here's one way to do it:

$ for file in ifcfg*; do
    num=$(grep IPADDR $file|awk -F. '{print $4}')
    sed -e "s/10.1.1.[0-9]\+/10.2.2.$(($num-19))/" $file >tmp/$file

This takes the contents of the IP address and cut's it down so that it's the 4th octet, which is stored in the variable $num. This variable is then added with 19 during the sed command. I needed to expand your original sed a bit by adding a [0-9]\+ to remove the existing 4th octet so that we could just replace it with $num+19.

share|improve this answer
Thanks, but got error message "sed: -e expression #1, char 45: unterminated `s' command" – rookie Jul 2 '13 at 15:21
@rookie - can you share what the lines look like that contain the IP addresses? Are you running this under /etc/sysconfig/network-scripts/ifcfg*? – slm Jul 2 '13 at 15:23
the lines look like IPADDR=10.1.1.x and yes, sort of, I want to change in /etc/sysconfig/network-scripts/ifcfg*, but am playing in a copied folder so far – rookie Jul 2 '13 at 15:29
Great, thanks a lot!!! Just what I needed when left alone at the office... Lots of sunshine to you :-) – rookie Jul 2 '13 at 15:51
@rookie - thanks please mark it as the accepted answer and upvote if you think it was helpful and the most appropriate answer for your question, so others know that you issue's been resolved. Same to you! – slm Jul 2 '13 at 15:54

You will have better luck doing this with awk rather than sed.

awk -F. '{printf ("%d.%d.%d.%d\n",$1,$2,$3,$NF-19)}'

or as per your example,

awk -F. '/10.1.1/ {printf ("10.2.2.%d\n",$1,$2,$3,$NF-19)}'


for file in ifcfg*
    awk -F. '/10.1.1/ {printf ("10.2.2.%d\n",$1,$2,$3,$NF-19)}' $file >tmp/$file

To preserve the other lines and assuming that x is the last element on the line to be replaced, here is what you do

for file in ifcfg*
    awk -F. '/10.1.1/ {printf ("%s.%d.%d.%d\n",$1,2,2,$NF-19)}
             !/10.1.1/ {print $0}' $file > tmp/$file
share|improve this answer
Thanks, but this gives me a bunch of files that are all the same: a line with just and also I need to keep other lines in the original file, these are now overwritten with the awk. (With my sed command above, the old content was left, only the ip adress was changed) – rookie Jul 2 '13 at 15:07
Do you have other content on the same line? You may have to tweak the print statement to preserve that content. – unxnut Jul 2 '13 at 15:16
the other content is on other lines, and the line I want to change is really IPADDR=10.1.1.x – rookie Jul 2 '13 at 15:25
I just modified the answer above to take care of this. – unxnut Jul 2 '13 at 15:49
OK, thanks, this works too. This forum is a great way to get help! Thank you! – rookie Jul 2 '13 at 16:02
awk 'BEGIN{FS=OFS="."} {$NF -= 19; print $0 > ("tmp/" FILENAME)}' ifcfg*
share|improve this answer

I'd use perl for this:

$ grep '10\.' file[123]

$ perl -i -pe 's/10\.1\.1\.(\d+)/ "10.2.2." . ($1 - 19) /ge' file[123]

$ grep '10\.' file[123]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.