Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This seems to work:</p> <pre><code>awk -F, '{ p1 = substr($4, 1, 6); p2 = ("0x" substr($4, 7, 4)) + 0; p3 = ("0x" substr($4, 11, 4)) + 0; printf "%s,%s,%s,%s,%d,%d\n", $1, $2, $3, p1, p2, p3; }' </code></pre> <p>For your sample input data, it produces:</p> <pre><code>123711184642,02,3583090366663629,639f02,292,14292 123715942138,01,3538710295145500,639f02,45014,50755 123711616258,02,3548370476972758,639f02,72,22322 </code></pre> <p>The string concatenation of '0x' plus the 4-digit hex followed by adding 0 forces <code>awk</code> to treat the numbers as hexadecimals.</p> <p>You can simplify this to:</p> <pre><code>awk -F, '{ p1 = substr($4, 1, 6); p2 = "0x" substr($4, 7, 4); p3 = "0x" substr($4, 11, 4); printf "%s,%s,%s,%s,%d,%d\n", $1, $2, $3, p1, p2, p3; }' </code></pre> <p>The strings prefixed with 0x are forced to integer when presented to <code>printf()</code> and the <code>%d</code> format.</p> <hr> <p>The code above works beautifully with the native <code>awk</code> on MacOS X 10.6.5 (version 20070501); sadly, it does not work with GNU <code>gawk</code> 3.1.7. That, it seems, is permitted behaviour according to POSIX (see the comments below). However, <code>gawk</code> has a non-standard function <code>strtonum</code> that can be used to bludgeon it into performing correctly - pity that bludgeoning is necessary.</p> <pre><code>gawk -F, '{ p1 = substr($4, 1, 6); p2 = "0x" substr($4, 7, 4); p3 = "0x" substr($4, 11, 4); printf "%s,%s,%s,%s,%d,%d\n", $1, $2, $3, p1, strtonum(p2), strtonum(p3); }' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload