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

Hash table related syntax errors in an AWK script? [closed]

提问人:CApple 提问时间:10/7/2023 最后编辑:markp-fusoCApple 更新时间:10/8/2023 访问量:60

问:


这个问题是由错别字或无法再现的问题引起的。虽然类似的问题可能在这里是主题,但这个问题的解决方式不太可能帮助未来的读者。

上个月关闭。

我在尝试运行下面包含的 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 语法错误 哈希表

评论

1赞 John Bayko 10/7/2023
awk 不会像现代语言那样自动表示变量 - 也就是说,是一个变量,是变量的内容,并且您希望按变量的内容进行索引。last_name$last_name
1赞 markp-fuso 10/7/2023
注释指出,这 - - 用于初始化哈希表;你从哪里得到这个?在我的 This 中,调用名为 (用户定义的) 函数并将其返回值分配给名为 () 标量变量的dealer_schedule = assoc()awkassocawkdealer_schedule
1赞 markp-fuso 10/7/2023
dealer_schedule[last_name] = [date, time, am_pm, first_name, last_name]是无效的赋值语句(例如,右侧的 和 是无效的语法);根据后来的参考资料(例如,)我猜您实际上是在尝试填充数组的第二维,但是......a) 这不是填充数组的第 2 维的方式,b) 尝试引用数组的第 3 维(但您从未填充第 3 维[]=dealer_schedule[dealer][i][0]dealer_scheduledealer_schdule[dealer][i][0]
1赞 markp-fuso 10/7/2023
fields = split($0, fields, "|")说要拆分管道分隔符 () 上的输入行 () 并用结果填充数组......它还说将数组条目的计数/数量(即函数调用的输出)分配给名为 ;这应该会生成一个致命错误,指出不能同时用作标量变量和数组变量$0|fields[]split()fieldsfields
2赞 Ed Morton 10/8/2023
@JohnBayko关于last_name是一个变量,$last_name 是变量的内容”,- 不,您可能正在考虑 shell 而不是 awk,因为 shell 也是如此,但在 awk 中要获取变量的内容,您只需使用它的名称,就像在 C、python 等中一样。last_name

答:

2赞 Daweo 10/8/2023 #1

你的代码是 GNU 语法和语法的奇怪混合体,随意收集了 1986 年的编程语言和 1991 年的不相关的编程语言的部分,根本没有机会工作,没有说出所需的输出。AWKpython

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

此特定行使用列表文字。GNU 没有列表,只有关联数组,它们大致相当于 python 的字典。您需要将其表示为数组,键为数字。你还需要更隐含,因为据我所知,GNU 没有 Array 字面量,所以如果你可以做类似的事情AWKAWKpython

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

在 GNU 中需要AWK

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

通常,您可以尝试将此代码转换为正确的代码,或者将此代码转换为正确的 GNU 代码,但首先请查阅您组织的规则,以检查这些工具之一是否不是 verboten。pythonAWK

评论

0赞 Ed Morton 10/8/2023
在任何 awk 中,包括 GNU,那将是 .如果需要一个 2d 数组,那么在 GNU awk 中,它将是 .实际上,应该从 awk 开始而不是从 awk 开始,以符合所有生成的数组。你是对的,没有 awk 有数组文字。split("a b c",x)x[0][1]; split("a b c",x[0])x[]10