2016-02-11 143 views
0

我想出了下面的脚本,它列出了服务器日志文件中需要超过10000000微秒(ServiceDuration在我们的日志中记录的值以微秒记录的值)的呼叫,该文件存储所有来到服务的呼叫。AWK意外的令牌错误

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 
+0

该输出与给定示例日志应该如何? – jkdba

+0

@JohnK - 用期望的输出更新了文章。 – vk239

回答

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");' 

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

#get slow running service's 
+0

这解决了它。看起来'gawk'不允许在评论中使用''''。同样的脚本在'awk'的BusyBox v1.19.2'实现上运行得很好。 – vk239

+2

然后BusyBox v1.19.2的外壳坏了。这种行为与awk无关 - 无论shell执行脚本的工具是awk还是sed或grep或其他任何内容,shell都不允许在单引号分隔的脚本中的任何位置使用单引号。 –