home | list info | list archive | date index | thread index

Re: [OCLUG-Tech] How to convert a character MAC address into individual hex bytes for writing?

On 2017-07-28 09:06 AM, Stephen M. Webb wrote:
> 
> You could also go for sed instead.
> 
> $ for c in $(echo A0:B1:C2:D3:E4:F5 | sed 's/\([[:xdigit:]]\{2,2\}\)[^[:xdigit:]]*/ 0x\1/g'); do echo $c; done
> 0xA0
> 0xB1
> 0xC2
> 0xD3
> 0xE4
> 0xF5
> 
> but the readability of that is questionable.

The readability of the sed expression is improved marginally if you switch to using extended regular expressions instead
of basic regular expressions.

$ echo A0:B1:C2:D3:E4:F5 | sed -r 's/([[:xdigit:]]{2,2})[^[:xdigit:]]*/\1 /g'
A0 B1 C2 D3 E4 F5

There is no performace difference between using BREs and EREs in this case, and using sed is marginally faster than
using grep over several test runs on my machine.

Note that only the sed version (either BRE or ERE) is POSIX.  "grep -o" is a GNU extension.

-- 
Stephen M. Webb  <stephen [ dot ] webb [ at ] bregmasoft [ dot ] ca>