[英]Convert bash line to use in perl
我如何将以下bash行转换为perl? 我可以运行system()命令,还是有更好的方法? 我正在寻找perl,以便每天从我的apache access_log文件中打印出访问权限。
在bash中:
awk '{print $4}' /etc/httpd/logs/access_log | cut -d: -f1 | uniq -c
打印以下内容:
632 [27/Apr/2014
156 [28/Apr/2014
awk '{print $4}' /etc/httpd/logs/access_log | cut -d: -f1 | uniq -c
perl -lane'
($val) = split /:/, $F[3]; # First colon-separated elem of the 4th field
++$c{$val}; # Increment number of occurrences of val
END { print for map { "$c{$_} $_" } keys %c } # Print results in no order
' access.log
开关:
-l
自动将换行符附加到print语句。 -l
还从-n
(和-p
)读取的行中删除换行符。 -a
将空白行分割为数组@F
。 -n
循环输入的行,但不打印每行。 -e
执行给定的脚本主体。 您的原始命令已翻译为Perl一线式:
perl -lane '($k) = $F[3] =~ /^(.*?):/; $h{$k}++ }{ print "$h{$_}\t$_" for keys %h' /etc/httpd/logs/access_log
您可以从以下位置将所有命令更改为一个:
awk '{print $4}' /etc/httpd/logs/access_log | cut -d: -f1 | uniq -c
至
awk '{split($4,a,":");b[a[1]]++} END {for (i in b) print b[i],i}' /etc/httpd/logs/access_log
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.