Terminal Command to replace text in a text file with the MAC address of the machine


I have a text document that has a machine's MAC address in it. I need to copy this file to multiple machines and would like to have a terminal script/command to replace the old MAC with the MAC of the new machine. I am using OSX 10.6 but I'm sure most Linux commands would work as well. I don't want to use a perl script or anything like that if I can avoid it. A basic UNIX command would be great. Can anyone help me or steer me in the right direction?

Thanks for all your help.
Who is Participating?
AriMcConnect With a Mentor Commented:
You could also try making the file path more absolute like this:

~$ sed -i "s/00:25:00:cf:2d:8e/$MACADDR/g" ~/Desktop/testmanaged.plist

If your textfile /tmp/test.txt has a placeholder MACADDR for the MAC-address, then this could work:

export MACADDR=`ifconfig | grep eth | awk '{print $5};'`
sed -i "s/MACADDR/$MACADDR/g" /tmp/test.txt

You might need to experiment if your NICs show up as something else than ethX or if your Linux-tools output formats are different from my Debian.

Oh, one more thing: make sure your ifconfig command directory is in your path-variable or use an absolute reference (/sbin/ifconfig in Debian).

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

WindhamSDAuthor Commented:
Thanks Ari,

Here is the part of the document that I want to autochange in bold italics:


Here is the output of my ifconfig:

lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
      inet6 ::1 prefixlen 128
      inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
      inet netmask 0xff000000
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
      ether 00:25:00:a1:05:1a
      inet6 fe80::225:ff:fea1:51a%en0 prefixlen 64 scopeid 0x4
      inet netmask 0xffff0000 broadcast
      media: autoselect (100baseTX <full-duplex>)
      status: active

I always want it to update with the en0 address. Will the same commands you posted work in this instance. I use terminal all the time to issue commands, but not at the level of making and replacing text.

I think this would work in your case:

export MACADDR=`ifconfig | grep ether | awk '{print $2};'`
sed -i "s/00:25:00:cf:2d:8e/$MACADDR/g" /tmp/test.txt

WindhamSDAuthor Commented:
I think we are getting close Ari,

This is what I entered and the error I received:

~$ export MACADDR=`ifconfig | grep ether | awk '{print $2};'`
~$ sed -i "s/00:25:00:cf:2d:8e/$MACADDR/g" Desktop/testmanaged.plist
sed: 1: "Desktop/testmanaged.plist": extra characters at the end of D command

Any ideas? Thanks again.
Looks like it's reading the filename Desktop/... as an argument.  Try this without the -i:

sed "s/00:25:00:cf:2d:8e/$MACADDR/g" Desktop/testmanaged.plist >newfile

If that works, then move or copy the newfile over the original:

sed "s/00:25:00:cf:2d:8e/$MACADDR/g" Desktop/testmanaged.plist >newfile &&
cp -f newfile Desktop/testmanaged.plist
WindhamSDAuthor Commented:
Bingo! Absolute path...this wouldn't have been an issue because the real location of the file is buried in the /var/ directory, but I was testing it on my desktop. Appreciate all the help!
WindhamSDAuthor Commented:
Thank You for your input too burniep
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.