我如何将以下bash行转换为perl?我可以运行system()命令,还是有更好的方法?我正在寻找perl来从我的apache access_log文件中打印出每天的访问权限.
在bash中:
awk '{print $4}' /etc/httpd/logs/access_log | cut -d: -f1 | uniq -c
Run Code Online (Sandbox Code Playgroud)
打印以下内容:
632 [27/Apr/2014
156 [28/Apr/2014
Run Code Online (Sandbox Code Playgroud)
awk '{print $4}' /etc/httpd/logs/access_log | cut -d: -f1 | uniq -c
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
开关:
-l 自动在print语句中附加换行符.-l还会从-n(和-p)读取的行中删除换行符.-a将空格上的行拆分为数组@F.-n 循环输入行但不打印每一行.-e 执行给定的脚本体.