awk语法错误"表达式中的无效字符"

时间:2014-06-05 07:24:22

标签: awk

我有一个如下的脚本

#!/bin/ksh
while read -r line
do
        cname=$(echo ${line} | awk -F"[ ]" '{print $10}')
        scode=$(echo ${line} | awk -F"[ ]" '{print $(NF-1)}')
        [[ ( ${scode} -ge 200 ) && ( ${scode} -le 399 ) ]] && {
                echo ${line} >> ${cname}_access.log
                }
        [[ ( ${scode} -ge 400 ) && ( ${scode} -le 599 ) ]] && {
                echo ${line} >> ${cname}_error.log
                }
done < /home/vizion/Desktop/adn_DF9D_20140515_0001.log

我正在Error "line 6: :: invalid character in expression - +http://www.google.com/bot.html)""

我的日志

66.249.75.49 - - [15/May/2014:00:12:01 +0000] "GET abc.def.com/80DF9D/plantronics/us/support/software-downloads/download.jsp HTTP/1.1" 200 3956 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" "-"

请纠正我错在哪里

2 个答案:

答案 0 :(得分:0)

我不确定这是你想要的。我用过这个:

awk '{scode=$9;cname=substr($7,1,index($7,"/")-1);
      if(scode >= 200 && scode < 399){print >> cname"_access.log"}
      else{if(scode >= 400 && scode < 599){print >> cname"_error.log"}}}' logfile

日志文件:

66.249.75.49 - - [15/May/2014:00:12:01 +0000] "GET abc.def.com/80DF9D/plantronics/us/support/software-downloads/download.jsp HTTP/1.1" 200 3956 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" "-"

它创建了一个名为abc.def.com_access.log的文件。该文件的内容是:

66.249.75.49 - - [15/May/2014:00:12:01 +0000] "GET abc.def.com/80DF9D/plantronics/us/support/software-downloads/download.jsp HTTP/1.1" 200 3956 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" "-"

答案 1 :(得分:0)

使用以下脚本

#!/bin/ksh
while read -r line
do
        cname=$(echo ${line} | awk '{split($7,c,"/"); print c[3]}')
        scode=$(echo ${line} | awk -F"[ ]" '{print $9}')
        [[ ( ${scode} -ge 200 ) && ( ${scode} -le 399 ) ]] && {
                echo ${line} >> ${cname}_access.log
                }
        [[ ( ${scode} -ge 400 ) && ( ${scode} -le 599 ) ]] && {
                echo ${line} >> ${cname}_error.log
                }
done < /home/vizion/Desktop/adn_DF9D_20140515_0001.log