Reputation: 137
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?
Upvotes: 1
Views: 1331
Reputation: 204054
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.
Upvotes: 1
Reputation: 6300
You can use printf for this.
Instead of {$1=$1}
you can have { printf("%6s;%7s;%4s;...", $1, $2, $3, ...) }
(replace "..." with more fields)
Also it seems that in your output FIELDWIDTHS didn't work, because it looks as if it was just separated by whitespace, and not by column lengths. Make sure that you use the right version of gawk.
Also make sure the the field widths are right. The first 4 fields should probably be "7 7 5 3...", not "6 7 4 2..." to include spaces.
Upvotes: 0