简体   繁体   English

OSX,G / AWK,Bash-“非法声明,未终止的字符串”,没有文件输出

[英]OSX, G/AWK, Bash - “illegal statement, unterminated string” and no file output

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. 我有一个SO的人提供的脚本来解决我遇到的问题,但是,我在使它在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. 从它的外观来看,[i]变量并未修改为输出文件,但我不知道为什么。

If I change AWK to GAWK and run the original script here is the output: 如果将AWK更改为GAWK并运行原始脚本,则输出为:

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: 因此,对于L1,输出示例如下:

EXAMPLEfoo,60,6
EXAMPLEbar,30,6
EXAMPLE1,60,3
EXAMPLE2,120,6
EXAMPLE3,60,6
EXAMPLE4,30,6

And for L2: 对于L2:

EXAMPLEfoo,60,0
EXAMPLEbar,30,6
EXAMPLE1,60,3
EXAMPLE2,120,0
EXAMPLE3,60,6
EXAMPLE4,30,6

I see two problems (on OS X platform): 我看到两个问题(在OS X平台上):

  1. The awk command on OS X does not support the -v flag. OS X上的awk命令不支持-v标志。 We can fix it by using the BEGIN pattern. 我们可以使用BEGIN模式修复它。
  2. The OS X awk does not like the way output file constructed in the print line. OS X awk不喜欢在打印行中构造输出文件的方式。

Here is my solution, which seems to work both on Mac OS X Snow Leopard and Red Hat Linux 4.x: 这是我的解决方案,它似乎在Mac OS X Snow Leopard和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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM