On 2017-07-28 07:22 AM, rpjday [ at ] crashcourse [ dot ] ca wrote:
>
> OK, this exercise should be a bit trickier -- given a character MAC
> address, how can I break it into individual hex chars for writing to
> EEPROM? So the script takes the argument, say, "A0:B1:C2:D3:E4:F5",
> and I need to, one nibble at a time, get the value 0x0A, 0x0, 0x0B, 0x01
> and so on.
>
> (Doesn't matter if the invoker wants to put the colons in or not, I
> can just run the string through "tr -d ':'" to deal with that.)
>
> As earlier, I can always loop through each character in the MAC address,
> but I'm wondering if there's some clever "printf" format that can
> do that all at once.
The closest I can get is this.
$ for c in $(echo A0:B1:C2:D3:E4:F5 | grep -o '[[:xdigit:]]\{2\}'); do echo 0x$c; done
0xA0
0xB1
0xC2
0xD3
0xE4
0xF5
Works if you include the colons or not.
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.
--
Stephen M. Webb <stephen [ dot ] webb [ at ] bregmasoft [ dot ] ca>