AWK 意外的令牌错误

AWK Unexpected token error

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

servicedurationlimit.awk

#!/bin/sh
dir1=/*.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 ( == "#BeginLogEntry")
                {
                        split([=10=], a, "\t");
                        servinfo=a[3]" ServiceAt:"a[2];
                } else {
                        getline;
                        if ([=10=] > 10000000)
                        {
                                print servinfo", ServDur:"[=10=]
                        }
                }
        }' $file1
done

在 运行 脚本中出现以下错误:

./servicedurationlimit.awk /path/to/server/logs/
./servicedurationlimit.awk: line 12: syntax error near unexpected token `[=11=],'
./servicedurationlimit.awk: line 12: `                  split([=11=], 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

我是 运行 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 `[=10=],'
./test.sh: line 11: `        split([=10=], a, "\t");'

不过我相信您只需要从您的评论中删除 ' 单引号:

#get slow running service's