Converting hex to decimal in awk or sed

前端 未结 7 1499
南方客
南方客 2020-12-06 16:13

I have a list of numbers, comma-separated:

123711184642,02,3583090366663629,639f02012437d4
123715942138,01,3538710295145500,639f02afd6c643
123711616258,02,35         


        
7条回答
  •  一生所求
    2020-12-06 16:35

    Perl version, with a tip of the hat to @Jonathan:

    perl -F, -lane '$p1 = substr($F[3], 0, 6); $p2 = substr($F[3], 6, 4); $p3 = substr($F[3], 10, 4); printf "%s,%s,%s,%s,%d,%d\n", @F[0..2], $p1, hex($p2), hex($p3)' file
    

    -a turn on autosplit mode, to populate the @F array
    -F, changes the autosplit separator to , (default is whitespace)
    The substr() indices are 1 less than their awk equivalents, since Perl arrays start from 0.

    Output:

    123711184642,02,3583090366663629,639f02,292,14292
    123715942138,01,3538710295145500,639f02,45014,50755
    123711616258,02,3548370476972758,639f02,72,22322
    

提交回复
热议问题