AWK意外的令牌错误

vk2*_*239 0 bash awk gawk

我想出了下面的脚本来列出超过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
Run Code Online (Sandbox Code Playgroud)

在运行脚本时,我得到以下错误:

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

你能帮我理解可能导致这种情况的原因吗?

下面是示例日志文件(有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
Run Code Online (Sandbox Code Playgroud)

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

BROWSE_SOME ServiceAt:04.13 21:12:11.671, ServDur: 162818123
Run Code Online (Sandbox Code Playgroud)

awk 版本信息

$ awk --version
GNU Awk 3.1.5
Run Code Online (Sandbox Code Playgroud)

jkd*_*dba 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 `$0,'
./test.sh: line 11: `        split($0, a, "\t");'
Run Code Online (Sandbox Code Playgroud)

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

#get slow running service's
Run Code Online (Sandbox Code Playgroud)

  • 然后BusyBox v1.19.2的shell坏了.这种行为与awk无关 - 无论shell执行脚本的工具是awk还是sed或grep还是其他任何东西,shell都不允许在单引号分隔的脚本中的任何地方使用单引号. (2认同)