awk的语法错误:AWK:行29:语法错误或接近:

问题描述:

我已经写了awk剧本,我不断收到以下错误:awk的语法错误:AWK:行29:语法错误或接近:

awk: line 29: syntax error at or near :

我不明白为什么我一直在得到这个错误。

该脚本是下面(脚本是大但误差仅在顶部部分,只要添加的完整性的脚本。一个标志已被标记为行了错误)。

#!/bin/sh 

tshark -V -r $1 > .pcap_out1_ver.txt 
tshark -r $1 > .pcap_out_summ.txt 

awk -F ":" ' 
BEGIN { 
#Packet types and subtypes. 
    frame_id[0] = "Association Request"; 
    frame_id[1] = "Association Response"; 
    frame_id[2] = "Association Response"; 
    frame_id[3] = "Reassociation Response"; 
    frame_id[4] = "Probe Request"; 
    frame_id[5] = "Probe Response"; 
    frame_id[6] = "Reserved"; 
    frame_id[7] = "Reserved"; 
    frame_id[8] = "Beacon"; 
    frame_id[9] = "ATIM"; 
    frame_id[10] = "Disassociation"; 
    frame_id[11] = "Authentication"; 
    frame_id[12] = "Deauthentication"; 
    frame_id[13] = "Action"; 
    for(x=14; x<24; ++x) { 
     frame_id[x] = "Reserved"; 
    } 
    frame_id[24] = "Block Ack Request"; 
    frame_id[25] = "Block Ack"; 
    frame_id[26] = "PS-Poll"; 
    frame_id[27] = "RTS";  #******Error here**** 
    frame_id[28] = "CTS"; 
    frame_id[29] = "ACK"; 
    frame_id[30] = "CF-end"; 
    frame_id[31] = "CF-end + CF-ack"; 
    frame_id[32] = "Data"; 
    frame_id[33] = "Data + CF-ack"; 
    frame_id[34] = "Data + CF-poll"; 
    frame_id[35] = "Data + CF-ack +CF-poll"; 
    frame_id[36] = "Null"; 
    frame_id[37] = "CF-ack"; 
    frame_id[38] = "CF-poll"; 
    frame_id[39] = "CF-ack + CF-poll"; 
    frame_id[40] = "QoS data"; 
    frame_id[41] = "QoS data + CF-ack"; 
    frame_id[42] = "QoS data + CF-poll"; 
    frame_id[43] = "QoS data + CF-ack + CF-poll"; 
    frame_id[44] = "QoS Null"; 
    frame_id[45] = "Reserved"; 
    frame_id[46] = "QoS + CF-poll (no data)"; 
    frame_id[47] = "Qos + CF-ack (no data)"; 
    packet_type[0] = "Management"; 
    packet_type[1] = "Control"; 
    packet_type[2] = "Data"; 
#Variables for storing stats. 
    captured_length = 0; 
    for(x=0; x<50; ++x) { 
     count[x]=0; 
     traffic[x]=0; 
    } 
#Counter for Epoch Time. Avg data rates. 
    next_mark=0; 
    j=0; 
    first_epoch_time = 0; 
    cur_epoch_time = 0; 
#Counter for rssi values. 
    k=0; 
} 
{ 
    gsub(/^[ \t]+/, "", $1); 
    if($1=="Frame Control") { 
     gsub(/^[ \t]+/, "", $2); 
     intRep = sprintf("%d", "0x" substr($2, 4, 2)); 
     traffic[intRep] += captured_length; 
     count[intRep] += 1; 
    } else if($1=="Capture Length") { 
     gsub(/^[ \t]+/, "", $2); 
     gsub(/ [^\0]*/,"", $2); 
     captured_length = $2; 
    } else if($1=="Epoch Time") { 
     gsub(/^[ \t]+/, "", $2); 
     gsub(/ [^\0]*/, "", $2); 
     if(next_mark<$2) { 
      if(next_mark==0) { 
       next_mark = $2+60; 
       first_epoch_time = $2; 
      } else { 
       next_mark += 60; 
       j++; 
      } 
      #initialization of array element before using. 
      traffic_per_min[j] = 0; 
      count_per_min[j] = 0; 
      data_rate[j] = 0; 
     } 
     cur_epoch_time = $2; 
     traffic_per_min[j] += captured_length; 
     count_per_min[j] += 1; 
    } else if($1=="SSI signal") { 
     gsub(/^[ \t]+/, "", $2); 
     print "ssi signal" 
     if(substr($2, 0, 1) == "-") { 
      rssi_v[k] = $2; 
      rssi_t[k] = cur_epoch_time; 
      print rssi_v[k]; 
      print rssi_t[k]; 
      k++; 
     } 
    } else if($1=="Data Rate") { 
     gsub(/^[ \t]+/, "", $2); 
     gsub(/ [^\0]*/, "", $2); 
     data_rate_avg[j] += $2; 
     data_rate[k] = $2; 
    } 
} 
END { 
# print "Packet Subtype" "No. of Packets" "Amount of traffic" 
    for(x=0; x<48; ++x) { 
     if(count[x] != 0) { 
      print frame_id[x]":"count[x]":"traffic[x]; 
     } 
    } 
    print "-----" 
    for(x=0; x<=j; ++x) { 
     print x traffic_per_min[x]/count_per_min[x]; 
    } 

} 
' .pcap_out1_ver.txt > .parsed.txt 

awk -F " \t" ' 
BEGIN { 
    for(x=0; x<6; ++x) 
     count[6] = 0; 
    protocol[0] = "HTTP"; 
    protocol[1] = "ARP"; 
    protocol[2] = "SMTP"; 
    protocol[3] = "DNS"; 
    protocol[4] = "FTP"; 
    protocol[5] = "DHCP"; 
} 
{ 
    if($5==protocol[0]){ 
     count[0] += 1; 
    } else if($5==protocol[1]) { 
     count[1] += 1; 
    } else if($5==protocol[2]) { 
     count[2] += 1; 
    } else if($5==protocol[3]) { 
     count[3] += 1; 
    } else if($5==protocol[4]) { 
     count[4] += 1; 
    } else if($5==protocol[5]) { 
     count[5] += 1; 
    } 
} 
END { 
    for(x=0; x<6; ++x) { 
     print protocol[x]:count[x] 
    } 
} 
' .pcap_out_summ.txt > .app_net.txt 

你此行到底块:

print protocol[x]:count[x] 

它应改为:

print protocol[x]":"count[x] 
+0

我只是错过了冒号。为什么这会给出错误的行号呢? – 2013-02-08 19:18:41

+0

它必须是第二个awk脚本的第#行。 – anubhava 2013-02-08 19:30:29

+4

@AmanDeepGautam - 要知道,你贴得shell脚本,而不是一个awk脚本。在这个shell脚本中你有2个awk脚本。在解析该脚本(第二个在文件中)发现,语法错误,它告诉你它发现在这一点AWK脚本的行数其中一个AWK的脚本中有一个语法错误和AWK解释。 – 2013-02-08 20:03:27

既然awk告诉你,这条线你awk脚本是错的:

print protocol[x]:count[x] 

你大概意思打印冒号:

print protocol[x] ":" count[x] 

除了你的语法错误,我可以做一个关于你的awk脚本的建议或2:

  1. 摆脱所有的空语句(虚假跟踪 分号)。
  2. 你似乎并没有把握awks关联数组的功能。以第二个脚本为例。它可以被重新写为刚:

    awk -F " \t" ' 
        BEGIN { n=split("HTTP ARP SMTP DNS FTP DHCP",protocol,/ /) } 
        { count[$5]++ } 
        END { for(x=0;x<n;++x) print protocol[x]":"count[protocol[x]]+0 } 
    ' .pcap_out_summ.txt > .app_net.txt 
    

你可能想看看这本书有效AWK编程,第三版阿诺德·罗宾斯(http://www.oreilly.com/catalog/awkprog3/)。