Search code examples
csvawkspacesfixed-width

How to preserve spaces in awk with fixed column width


I've got the following file:

Bonnr. Sessie Tijd  As okr                                        Bedrag  BTW-laag  BTW-hoog           
----------------------------------------------------------------------------------------------------- 
307717   5555 08:08 AS o                                            2,80      0,16                             
308670   5575 11:28 AS mut Supply Needs           kenmerk         -21,98                              
                           FIRM                                                                    
308671   5575 11:34 AP o                                            5,60      0,32                     

Wanted output:

Bonnr.;Sessie;Tijd ;As;okr;                                       ;Bedrag ;BTW-laag;  BTW-hoog           
------;------;-----;--;---;---------------------------------------;-------;--------;----------------- 
307717;  5555;08:08;AS;o  ;                                       ;  2,80 ;    0,16;                             
308670;  5575;11:28;AS;mut;Supply Needs           kenmerk         ;-21,98 ;        ;                    
      ;      ;     ;      ;FIRM                                   ;       ;        ;                 
308671;  5575;11:34;AP;o  ;                                       ;  5,60 ;    0,32;                  

I use the following command:

awk 'BEGIN{FIELDWIDTHS="6 7 4 2 4 39 7 8 8";OFS=","}{$1=$1}1' Test.txt > Test.csv

My output is:

Bonnr.,Sessie,Tijd,As,okr,Bedrag,BTW-laag,BTW-hoog
-----------------------------------------------------------------------------------------------------
307717,5555,08:08,AS,o,2,80,0,16
308670,5575,11:28,AS,mut,Supply,Needs,kenmerk,-21,98
FIRM
308671,5575,11:34,AP,o,5,60,0,32

I don't comprehend why awk doesn't preserve the spaces in column six. How can I solve this?


Solution

  • FIELDWIDTHS is gawk-specific and hasn't always been supported. You apparently aren't calling gawk or aren't calling a version of gawk that supports FIELDWIDTHS.

    This will work in any awk:

    $ cat tst.awk
    BEGIN { split("6 7 4 2 4 39 7 8 8",poss) }
    {
        for (i=1; i in poss; i++) {
            printf "%s;", substr($0,1,poss[i])
            $0 = substr($0,poss[i]+1)
        }
        print
    }
    
    $ awk -f tst.awk file
    Bonnr.; Sessie; Tij;d ; As ;okr                                    ;    Bed;rag  BTW;-laag  B;TW-hoog
    ------;-------;----;--;----;---------------------------------------;-------;--------;--------;----------------
    307717;   5555; 08:;08; AS ;o                                      ;      2;,80     ; 0,16   ;
    308670;   5575; 11:;28; AS ;mut Supply Needs           kenmerk     ;    -21;,98     ;        ;
          ;       ;    ;  ;    ;    FIRM                               ;       ;        ;        ;
    308671;   5575; 11:;34; AP ;o                                      ;      5;,60     ; 0,32   ;
    

    Just adjust your positions numbers to be what you really want.