Skip to content
Advertisement

How do I copy regex matches from a file? Need to get all MAC addresses from log file

I have a linux dhcpd log that I need to get a list of only the MAC addresses. The MAC addresses are formatted like 00:ab:27:d8:dd:dd

Using linux command line tools,parse INPUT file for MAC addresses and send to OUTPUT file. Where OUTPUT file is just a list of the MAC addresses, where then duplicate MAC addresses can be removed.

I suspect this might be a multi-step, complex command. I’ve searched the site and could not find a match for copy the results of regex search. I’ve had mixed results getting a reg-expression that works to even find the MAC addresses in the file, let alone copy out all of the right matches to a file.

Advertisement

Answer

You can use the following command to extract unique MAC addresses:

grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'  /var/log/dhcpd.log | uniq > unique_MAC.txt

Explanation:

  • This will retreive the MAC addresses from the log:

grep -o -E ‘([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}’

  • The uniq command will remove all duplicate MAC addresses:

uniq

References:
grep
uniq

Advertisement