unixawksedsplit

Split Characters into Several Parts


There'e many lines in a file, like the following:

000100667 ===> 000102833
005843000 ===> 005844000
011248375 ===> 011251958

I would like to split them into several parts so the result is as follows:

00:01:00,667 ===> 00:01:02,833
00:58:43,000 ===> 00:58:44,000
01:12:48,375 ===> 01:12:51,958

Right now I'm trying (success for the first part only):

echo 000100667 | gawk '{printf "%s:%s:%s,%s\n", $1, $2, $3, $4}' FIELDWIDTHS="2 2 2 3"


Solution

  • With your shown samples please try following awk code. Written and tested with GNU awk.

    awk -v OFS=":" '
    match($0,/^(.{2})(.{2})(.{2})(.{3})( ===> )(.{2})(.{2})(.{2})(.{3})$/,arr){
      print arr[1],arr[2],arr[3]","arr[4] arr[5] arr[6],arr[7],arr[8]","arr[9]
    }
    ' Input_file
    

    With FIELDWIDTHS solution: Written and tested with shown samples.

    awk -v FIELDWIDTHS="2 2 2 3 6 2 2 2 3" -v OFS=":" '{print $1,$2,$3","$4 $5 $6,$7,$8","$9}' Input_file