Issue
I have an awk command line developed in unix and I have to pass it to windows. I have installed the gawk tool for windows, I have configured my environment variable and finally I have converted the shell to batch but when I run the .bat it generates an empty file.
this is my code: file.bat
echo off
set "fecha=%1"
set "inputpath=%2"
set "unique=.uq"
set file=FICUO%fecha%.txt
echo %fecha%
echo %file%
awk "{!x[$1,$2,$6,$8,$9,$12,$16,$17,$33]++}" FIELDWIDTHS="3 19 19 3 3 5 12 6 9 40 4 8 13 3 3 3 3 1 3 13 13 13 13 13 13 3 13 2 8 8 8 2 8 6 11 11 11 8 4" %inputpath%%file% > %inputpath%%file%%unique%
I assume that the problem is the FIELDWITHS option, I have been researching about it and I find how to indicate the fixed position of the fields.
this is an example input flat file
2110004757960000000036000475796000332797200902200300000008067260007588013568776xxxxxxxxxxxxxxx 54112015123000000002621706040000080030000000000026217000000000327710000000032771000000000000000000000000000000000032771031000000000000025201603252016042000000000 20151230000000000000010000000000000000000001000000000000000
2310004757960000000036000475796000332797200902200300000008067260007588013568776xxxxxxxxxxxxxxx 54112015123000000002621706040000080040000000000026217000000000327710000000032771000000000000000000000000000000000032771030000000000000025201604252016052000000000 20151230000000000000010000000000000000000001000000000000000
any idea what could be my error so that it does not print anything in the output file, thanks
Solution
The problem is not FIELDWIDTHS
, it's that you have the condition that could trigger a print
buried in an action. Change:
{!x[$1,$2,$6,$8,$9,$12,$16,$17,$33]++}
to:
!x[$1,$2,$6,$8,$9,$12,$16,$17,$33]++
so the result is treated as a condition that could trigger the default print
action. I'm assuming the intent is to print unique lines from the input. FWIW we idiomatically name the array used in that context seen
rather than x
and it makes your code easier to understand if you declare your variables before they're used, i.e. at the start of the script, rather than after they're used:
awk -v FIELDWIDTHS="3 19 19 3 3 5 12 6 9 40 4 8 13 3 3 3 3 1 3 13 13 13 13 13 13 3 13 2 8 8 8 2 8 6 11 11 11 8 4" "!seen[$1,$2,$6,$8,$9,$12,$16,$17,$33]++" %inputpath%%file% > %inputpath%%file%%unique%
If you want the output to be tab-separated fields then:
awk -v FIELDWIDTHS="3 19 19 3 3 5 12 6 9 40 4 8 13 3 3 3 3 1 3 13 13 13 13 13 13 3 13 2 8 8 8 2 8 6 11 11 11 8 4" -v OFS="\t" "{key=$1 OFS $2 OFS $6 OFS $8 OFS $9 OFS $12 OFS $16 OFS $17 OFS $33} !seen[key]++{print key}" %inputpath%%file% > %inputpath%%file%%unique%
Sorry I don't know windows syntax so hopefully you know what to do with this if I got the quotes wrong or something.
Answered By - Ed Morton Answer Checked By - Gilberto Lyons (WPSolving Admin)