使用IF语句时AWK语法错误

时间:2017-03-29 18:10:16

标签: bash if-statement awk cygwin substring

虽然我以前使用过命令提示符/终端,但我对AWK很新。

我在下面有这个脚本,我根据国家代码和州代码创建数据子集。但是我收到语法错误。

BEGIN{
   FS = "\t"
   OFS = "\t"
   }

 # Subset data from the states you need for all years 
 if ($5 == "IN-GA" || $5 == "IN-DD" || $5 == "IN-DN" || $5 == "IN-KA" || $5 == "IN-KL" || $5 == "IN-MH" || $5 == "IN-TN" || $5 == "IN-GJ"){
        if (substr($17, 1, 4) == "2000"){
            print $5, $12, $13, $14, $15, $16, $17, $22, $23, $24, $25, $26, $28 > "Y2000_India_sampling_output.txt"
        }
    }   

在Cygwin上,我引用脚本,然后运行下面的代码行,你会立即看到语法错误:

$ gawk -f sampling_India.awk sampling_relFeb-2017.txt
gawk: sampling_India.awk:20:  gawk if ($5 == "IN-GA" || $5 == "IN-DD" || $5 == "IN-DN" || $5 == "IN-KA" || $5 == "IN-KL" || $5 == "IN-MH" || $5 == "IN-TN" || $5 == "IN-GJ"){
gawk: sampling_India.awk:20:       ^ syntax error

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您的if条件未包含在{...}块中。

有这样的话:

BEGIN {
   FS = OFS = "\t"
}
# Subset data from the states you need for all years 
$5 ~ /^IN-(GA|DD|DN|KA|KL|MH|TN|GJ)$/ && substr($17, 1, 4) == "2000" {
    print $5, $12, $13, $14, $15, $16, $17, $22, $23, $24, $25, $26, $28 > "Y2000_India_sampling_output.txt"
}

注意如何使用正则表达式将多个==条件合并为一个条件。