Quantcast
Channel: Output only MAC address on Ubuntu - Ask Ubuntu
Viewing all articles
Browse latest Browse all 9

Answer by terdon for Output only MAC address on Ubuntu

$
0
0

Here are a few ways:

  1. grep. There are various regular expressions that will pick these up. Here, I am looking for 5 repetitions of 2 letters or numbers followed by a colon, then any two characters. The -i makes the match case insensitive and the -o makes grep print only the matched portion. -E enables extended regular expressions. The same regex also works with PCREs (-P).

    ifconfig -a | grep -ioE '([a-z0-9]{2}:){5}..'
  2. sed. The -n suppresses normal output and the -r enables extended regular expressions. Using the same regex as above, this script will attempt to replace everything on the line with the part of it that matches the regex. If the substitution was successful, the resulting line is printed (because of the p at the end of the substitution).

    ifconfig -a | sed -rn 's/.*(([a-z0-9]{2}:){5}..).*/\1/p'
  3. awk. If the line starts with a word character ([a-zA-Z0-9_]), and has 5 fields, print the last one.

    ifconfig -a | awk '/^\w/&&NF==5{print $NF}'
  4. Perl, where, as usual, there are more than one ways to do it. This one is the same logic as the awk above. The -a tells perl to split each input line into the @F array.

    ifconfig -a | perl -lane 'if(/^\w/&&$#F==4){print $F[$#F]}'

    Alternatively, you can use the regex from the previous approaches:

    ifconfig -a | perl -lne '/(([a-z0-9]{2}:){5}..)/ && print $1'
  5. Coreutils.

    LANG_ALL=C ifconfig -a | grep 'HWadd' | tr -s '''\t' | cut -f 5

Viewing all articles
Browse latest Browse all 9

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>