2017-03-29 63 views
2

雖然我以前使用過命令提示符/終端,但我對AWK很新。使用IF語句時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 

有什麼想法?

回答

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" 
} 

說明如何使用正則表達式,你可以多==條件合併成一個條件。

+1

謝謝@anubhava。這樣可行!。我好奇。如果我不想在2000年對它進行分類,並且刪除'&& substr($ 17,1,4)==「2000」' - 我應該獲取所有涉及相關狀態的數據嗎?儘管所有年份? –

+0

是的,這是正確的 – anubhava