OSX, G/AWK, Bash - "illegal statement, unterminated string" and no file output.
        Posted  
        
            by S1syphus
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by S1syphus
        
        
        
        Published on 2010-04-13T13:39:24Z
        Indexed on 
            2010/04/13
            13:53 UTC
        
        
        Read the original article
        Hit count: 595
        
I have a script that somebody from SO kindly provided to solve an issue I was having, However, I'm having some issues getting it to work on OSX.
gawk --version
GNU Awk 3.1.6
awk --version
awk version 20100208
The original source is:
awk -F, -vOFS=, -vc=1 '
NR == 1 {
    for (i=1; i<NF; i++) {
        if ($i != "") {
            g[c]=i;
            f[c++]=$i
        }
    }
}
NR>2 {
    for (i=1; i < c; i++) {
        print $1,$2, $g[i] > "output_"f[i]".csv
    }
}' data.csv
When I run the script it gives the following error:
awk: syntax error at source line 12
context is print $1,$2, $g[i] > >>>  "output_"f <<< [i]".csv
awk: illegal statement at source line 13
From the look of it the variable of [i] isn't been amended to the output file, but I don't know why.
If I change AWK to GAWK and run the original script here is the output:
gawk: cmd. line:11:             print $1,$2, $g[i] > "output_"f[i]".csv
gawk: cmd. line:11:                                               ^ unterminated string
So I edit the relevant line to fix the unterminated string
print $1,$2, $g[i] > "output_"f[i]".csv"
Then it runs through fine produces no errors, but there is no output files.
Any ideas? I spent the majority of last night and this morning pouring over this.
A sample input file:
,,L1,,,L2,,,L3,,,L4,,,L5,,,L6,,,L7,,,L8,,,L9,,,L10,,,L11,
Title,r/t,needed,actual,Inst,needed,actual,Inst,needed,actual,Inst,needed,actual,Inst,neede d,actual,Inst,needed,actual,Inst,needed,actual,Inst,needed,actual,Inst,needed,actual,Inst,needed,actual,Inst,needed,actual,Inst
EXAMPLEfoo,60,6,6,6,0,0,0,0,0,0,6,6,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
EXAMPLEbar,30,6,6,12,6,7,14,6,6,12,6,6,12,6,8,16,6,7,14,6,7.5,15,6,6,12,6,8,16,6,0,0,6,7,14
EXAMPLE1,60,3,3,3,3,5,5,3,4,4,3,3,3,3,6,6,3,4,4,3,3,3,3,4,4,3,8,8,3,0,0,3,4,4
EXAMPLE2,120,6,6,3,0,0,0,6,8,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
EXAMPLE3,60,6,6,6,6,8,8,6,6,6,6,6,6,0,0,0,0,0,0,6,8,8,6,6,6,0,0,0,0,0,0,0,10,10
EXAMPLE4,30,6,6,12,6,7,14,6,6,12,6,6,12,3,5.5,11,6,7.5,15,6,6,12,6,0,0,6,9,18,6,0,0,6,6.5,13
And the example out put should be
So for L1 an example out put would look like:
EXAMPLEfoo,60,6
EXAMPLEbar,30,6
EXAMPLE1,60,3
EXAMPLE2,120,6
EXAMPLE3,60,6
EXAMPLE4,30,6
And for L2:
EXAMPLEfoo,60,0
EXAMPLEbar,30,6
EXAMPLE1,60,3
EXAMPLE2,120,0
EXAMPLE3,60,6
EXAMPLE4,30,6
        © Stack Overflow or respective owner