tags:

views:

218

answers:

1

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
+1  A: 

I see two problems (on OS X platform):

  1. The awk command on OS X does not support the -v flag. We can fix it by using the BEGIN pattern.
  2. The OS X awk does not like the way output file constructed in the print line.

Here is my solution, which seems to work both on Mac OS X Snow Leopard and Red Hat Linux 4.x:

awk -F, '
BEGIN { OFS=","; c=1 } # FIX problem 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++) {
        outfile=sprintf("output_%s.csv", f[i]) # FIX problem 2
        print $1,$2, $g[i] > outfile
    }
}' data.csv
Hai Vu
According to Apple's [man page for AWK](http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/awk.1.html), the `-v` option *is* supported. And the OP removed the redirection and still didn't get any output. Something *else* is going on.
Dennis Williamson
Dennis: Yes, the man page (at least on Snow Leopard) stated that -v is supported, but I tried a simple awk -vmyvar=5 '{BEGIN print myvar}'and it does not work, complaining "awk: invalid -v option"
Hai Vu
I'm assuming it's just a typo here, but the `BEGIN` belongs outside the braces. Try a space between the `-v` and `myvar` to see if that makes a difference
Dennis Williamson
Dennis: It was a typo. Inserting a space works: awk -v myvar=5 'BEGIN{print myvar}'. Thank you.
Hai Vu