Re: [OCLUG-Tech] How to convert a character MAC address into individual hex bytes for writing?
On Fri, Jul 28, 2017 at 07:22:16AM -0400, Robert P. J. Day 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. So would you like the bytes 0x0A, 0x00, etc., or the string "0x0A 0x00…"? Cheers, Alex Pilon ___ Linux mailing list Linux@lists.oclug.on.ca http://oclug.on.ca/mailman/listinfo/linux
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___ Linux mailing list Linux@lists.oclug.on.ca http://oclug.on.ca/mailman/listinfo/linux
Re: [OCLUG-Tech] How to convert a character MAC address into individual hex bytes for writing?
On 2017-07-28 07:22 AM, rpj...@crashcourse.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___ Linux mailing list Linux@lists.oclug.on.ca http://oclug.on.ca/mailman/listinfo/linux
Re: [OCLUG-Tech] How to convert a character MAC address into individual hex bytes for writing?
On 28/07/17 07:22 AM, rpj...@crashcourse.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. printf doesn't cut up a string in the way you need, it truncates it so not much help there. #given MAC=00:11:aa:bC:dE:0f mac=$(echo "${MAC,,}"|tr -d ':-') echo -n "$mac => " #you can echo "$mac"|sed 's/../0x& /g' #or for i in $(seq 0 2 10 );do echo -n "0x${mac:$i:2} ";done or any variant of from the character loop > > rday > > ___ > Linux mailing list > Linux@lists.oclug.on.ca > http://oclug.on.ca/mailman/listinfo/linux ___ Linux mailing list Linux@lists.oclug.on.ca http://oclug.on.ca/mailman/listinfo/linux