Monday, July 30, 2012

Using awk --non-decimal-data for hex calculations... - Stack Overflow

Converting hex to decimal in awk or sed - Stack Overflow:


I have a list of numbers, comma-separated:
123711184642,02,3583090366663629,639f02012437d4
123715942138,01,3538710295145500,639f02afd6c643
123711616258,02,3548370476972758,639f0200485732
I need to split the 3rd column into three as below:
123711184642,02,3583090366663629,639f02,0124,37d4
123715942138,01,3538710295145500,639f02,afd6,c643123711616258,02,3548370476972758,639f02,0048,5732
And convert the digits in the last two columns into decimal:
123711184642,02,3583090366663629,639f02,292,14292
123715942138,01,3538710295145500,639f02,45014,50755
123711616258,02,3548370476972758,639f02,72,22322
share|edit

53% accept rate
1
To the close-voter: this is a valid question about shell PROGRAMMING. – Jonathan Leffler Jan 6 '11 at 14:27
feedback

2 Answers


Here's a variation on Jonathan's answer:
awk $([[ $(awk --version) = GNU* ]] && echo --non-decimal-data) -F, '
    BEGIN {OFS = FS}
    {
        $6 = sprintf("%d", "0x" substr($4, 11, 4))
        $5 = sprintf("%d", "0x" substr($4,  7, 4))
        $4 = substr($4,  1, 6)
        print
    }'
I included a rather contorted way of adding the --non-decimal-data option if it's needed.
...

'via Blog this'

No comments:

Post a Comment