【问题标题】:AWK Unexpected token errorAWK 意外令牌错误
【发布时间】:2016-02-12 06:51:42
【问题描述】:

我想出了下面的脚本来列出服务器日志文件中花费超过 10000000 微秒(我们的日志中的 ServiceDuration 的值以微秒记录的值)的调用,该文件存储了所有进入服务的调用。

servicedurationlimit.awk

#!/bin/sh
dir1=$1/*.log

# for each log file in the input dir
for file1 in $dir1
do
        echo $file1":"
        awk  '/#BeginLogEntry|ServiceDuration/ {
                #get slow running service's information
                if ($1 == "#BeginLogEntry")
                {
                        split($0, a, "\t");
                        servinfo=a[3]" ServiceAt:"a[2];
                } else {
                        getline;
                        if ($0 > 10000000)
                        {
                                print servinfo", ServDur:"$0
                        }
                }
        }' $file1
done

运行脚本时出现以下错误:

./servicedurationlimit.awk /path/to/server/logs/
./servicedurationlimit.awk: line 12: syntax error near unexpected token `$0,'
./servicedurationlimit.awk: line 12: `                  split($0, a, "\t"); '

您能帮我了解一下是什么原因造成的吗?

以下是示例日志文件(有 2 个日志条目):

#BeginLogEntry  04.13 20:11:11.671  BROWSE_ALL
@Properties LocalData
IsJson=1
UserTimeZone=utc
@end
@IdcRSet AuditProps
2
auditPropertyKey
auditPropertyValue
ServiceDuration
62818
ServiceStartTime
{ts '2015-04-13 20:11:11.671'}
@end
#EndLogEntry    04.13 20:11:11.671  BROWSE_ALL
#BeginLogEntry  04.13 21:12:11.671  BROWSE_SOME
@Properties LocalData
IsJson=1
UserTimeZone=utc
@end
@IdcRSet AuditProps
2
auditPropertyKey
auditPropertyValue
ServiceDuration
162818123
ServiceStartTime
{ts '2015-04-13 21:12:11.671'}
@end
#EndLogEntry    04.13 21:12:11.671  BROWSE_SOME

以下是在包含上述日志条目的日志文件上运行脚本后我期望的输出。

BROWSE_SOME ServiceAt:04.13 21:12:11.671, ServDur: 162818123

awk版本信息

$ awk --version
GNU Awk 3.1.5

【问题讨论】:

  • 用给定的示例日志应该输出什么?
  • @JohnK - 用预期的输出更新了帖子。

标签: bash awk gawk


【解决方案1】:

我正在运行 GNU Awk 4.0.2,你的代码在我的盒子上给出了这个错误:

awk: cmd. line:2:       #get slow running services
awk: cmd. line:2:       ^ syntax error
./test.sh: line 11: syntax error near unexpected token `$0,'
./test.sh: line 11: `        split($0, a, "\t");'

但是我相信你只需要从你的评论中删除 ' 单引号:

#get slow running service's

【讨论】:

  • 这解决了它。看起来 gawk 不允许在 cmets 中使用 '。同样的脚本在awkBusyBox v1.19.2 实现上运行得非常好。
  • 然后 BusyBox v1.19.2 的外壳坏了。此行为与 awk 无关 - 无论执行脚本的工具是 awk、sed、grep 还是其他任何工具,任何 shell 都不允许在单引号分隔的脚本中的任何位置使用单引号。
猜你喜欢
  • 2011-12-20
  • 2015-11-19
  • 2018-08-27
  • 2014-12-07
  • 2016-08-10
  • 2013-02-11
  • 2013-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多