Issue
I have a list of mac addresses in this format:
412010000018
412010000026
412010000034
41:20:10:00:00:18
41:20:10:00:00:26
41:20:10:00:00:34
I tried this, but did not work:
sed 's/([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/\1:\2:\3:\4/g' mac_list
How should I do it?
Solution
You have to use the correct sed
syntax:
\{I\}
matches exactly I sequences (I is a decimal integer;
for portability, keep it between 0 and 255 inclusive).
\(REGEXP\)
Groups the inner REGEXP as a whole, this is used for back references.
Here is an example command that covers the first 2 fields
sed 's/^\([0-9A-Fa-f]\{2\}\)\([0-9A-Fa-f]\{2\}\).*$/\1:\2:/'
The following command can process a complete MAC address and is easy to read:
sed -e 's/^\([0-9A-Fa-f]\{2\}\)/\1_/' \
-e 's/_\([0-9A-Fa-f]\{2\}\)/:\1_/' \
-e 's/_\([0-9A-Fa-f]\{2\}\)/:\1_/' \
-e 's/_\([0-9A-Fa-f]\{2\}\)/:\1_/' \
-e 's/_\([0-9A-Fa-f]\{2\}\)/:\1_/' \
-e 's/_\([0-9A-Fa-f]\{2\}\)/:\1/'
Following the idea from a perl solution posted here by @Qtax with global substitutions one can get a shorter solution:
sed -e 's/\([0-9A-Fa-f]\{2\}\)/\1:/g' -e 's/\(.*\):$/\1/'
Answered By - Dima Chubarov Answer Checked By - Dawn Plyler (WPSolving Volunteer)