Print the IP address and the Mac address in the same line

ifconfig | head -n 2 | tr -d '\n' | sed -n 's/.*\(00:[^ ]*\).*\(adr:[^ ]*\).*/mac:\1 - \2/p'
Print the IP address and the Mac address in the same line
Sample Output
mac:00:50:12:BD:5E:5B - adr:

By: Koobiac
2014-09-03 14:35:27

1 Alternatives + Submit Alt

What Others Think

what os are you doing this on? tried on ubuntu and osx, both no go.
chrismccoy · 378 weeks ago
I had to use ifconfig wlan1 where wlan1 is the interface on my computer (most likely it'll be different on another computer) and change the part where it has adr to addr like this ifconfig wlan1 | head -n 2 | tr -d '\n' | sed -n 's/.*\(00:[^ ]*\).*\(addr:[^ ]*\).*/MAC:\1 - \2/p' ; echo Plus I added an empty line with "; echo" at the end.
ronin9999 · 377 weeks and 6 days ago
I also tried this on another computer but had to change the 00: to the starting hexadecimal values of the MAC address of my interface, like 14: instead. So the whole thing is ifconfig wlan0 | head -n 2 | tr -d '\n' | sed -n 's/.*\(14:[^ ]*\).*\(addr:[^ ]*\).*/MAC:\1 - \2/p' ; echo
ronin9999 · 377 weeks and 6 days ago
@Chrismccoy : on SLES (Suse) @Ronin : You can customize the syntax as needed, great job.
Koobiac · 377 weeks and 4 days ago
i tryed this : ifconfig | grep -A2 eth | tr -d "\n"| sed 's/\-\-/\n/g'|awk '{print "mac: "$5 " " $7}' | sed 's/addr:/addr: /g' and should work for all network interfaces.
mass1 · 377 weeks and 4 days ago
this version should work also for "wlan" interfaces : ifconfig | egrep -A2 "eth|wlan" | tr -d "\n"| sed 's/\-\-/\n/g'|awk '{print "mac: "$5 " " $7}' | sed 's/addr:/addr: /g'
mass1 · 377 weeks and 4 days ago
Thx for sharing ;)
Koobiac · 377 weeks and 3 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: