AWK 脚本中与哈希表相关的语法错误?

问题描述 投票:0回答:1

在尝试运行下面包含的 AWK 脚本时遇到这两个语法错误:

awk: syntax error at source line 30 source file test.awk
 context is
      dealer_schedule[last_name] = >>>  [ <<< date, time, am_pm, first_name, last_name]
awk: illegal statement at source line 30 source file test.awk
awk: syntax error at source line 33 source file test.awk

我在 Ubuntu Linux 上使用 awk 版本 3.4。这是脚本:


#!/usr/bin/awk -f

BEGIN {

  # Initialize the hash table to store the dealer schedule.
  dealer_schedule = assoc()

  # Initialize the hash table to store the number of times that Mylie was playing with each unique dealer.
  dealer_counts = assoc()

  # Read the Mylie player information file.
  mylie_info = read_mylie_info_file()

}

{
  # Get the date field from the first four characters of the file name.
  date = substr(FILENAME, 1, 4)

  # Split the input line into an array of fields.
  fields = split($0, fields, "|")

  # Get the time, AM/PM label, first name, and last name of the roulette dealer.
  time = fields[2]
  am_pm = fields[3]
  first_name = fields[4]
  last_name = fields[5]

  # Add the data to the dealer schedule hash table.
  dealer_schedule[last_name] = [date, time, am_pm, first_name, last_name]

  # For each time that Mylie was playing, increment the count for the dealer.
  for (i in mylie_info) {
    if ((date == mylie_info[i][0]) and (time == mylie_info[i][1]) and (am_pm == mylie_info[i][2])) {
      if (last_name in dealer_counts) {
        dealer_counts[last_name]++
      } else {
        dealer_counts[last_name] = 1
      }
    }
  }
}

END {
  # Print the number of times that Mylie was playing with each unique dealer to a separate txt file.

  print "dealer,count" > "Notes_Dealer_Analysis.txt"

  for (dealer in dealer_counts) {
    print dealer, dealer_counts[dealer] >> "Notes_Dealer_Analysis.txt"
  }

  # Print the 5 column dealer schedule for the times that Mylie was playing.

  print "date,time,am_pm,first_name,last_name" > "Dealers_working_during_losses.txt"

  for (dealer in dealer_schedule) {
    for (i in dealer_schedule[dealer]) {
      # Check if the dealer was working at the same time as Mylie was playing.
      if ((dealer_schedule[dealer][i][0] == mylie_info["date"]) and (dealer_schedule[dealer][i][1] == mylie_info["time"]) and (dealer_schedule[dealer][i][2] == mylie_info["am_pm"])) {
        print dealer_schedule[dealer][i][0], dealer_schedule[dealer][i][1], dealer_schedule[dealer][i][2], dealer_schedule[dealer][i][3], dealer_schedule[dealer][i][4] >> "Dealers_working_during_losses.txt"
      }
    }
  }

  # Close the Mylie player information file.
  mylie_info_file.close()

}

function read_mylie_info_file() {

  # Read the Mylie player information file.

  mylie_info = assoc()

  mylie_info_file = open("Mylie.txt", "r")

  for (line in mylie_info_file) {
    fields = split(line, fields, "|")

    date = fields[1]

    time = fields[2]

    am_pm = fields[3]

    mylie_info[i] = [date, time, am_pm]
  }

  mylie_info_file.close()

  return mylie_info

}

我尝试了各种使用不同语法结构的方法,这些语法结构应该适用于各种版本的 awk,包括这个版本。但我对 awk 脚本的了解有点太生疏了,我什至不知道我是否在问正确的问题或在正确的位置寻找。

awk syntax-error hashtable
1个回答
0
投票

你的代码是 GNU

AWK
语法和
python
语法的奇怪组合,1986 年编程语言部分的随意集合和 1991 年不相关的编程语言根本没有机会工作,更不用说期望的输出了。

awk: syntax error at source line 30 source file test.awk
...
dealer_schedule[last_name] = [date, time, am_pm, first_name, last_name]

此特定行使用列表文字。 GNU

AWK
没有列表,只有关联的 Arrays,大致相当于 python 的字典。您需要将其表示为数组,键为数字。据我所知,你还需要更加隐式,因为 GNU
AWK
没有数组文字,所以如果
python
你可能会做类似的事情

x = {0:"a",1:"b",2:"c"}

在 GNU 中

AWK
需要

x[0]="a";x[1]="b";x[2]="c"

通常,您可以尝试将此代码转换为正确的

python
代码,或将此代码转换为正确的 GNU
AWK
代码,但首先请查阅您组织的规则以检查这些工具之一是否不受禁止。

© www.soinside.com 2019 - 2024. All rights reserved.