[英]Convert bash line to use in perl
How would I go about converting the following bash line into perl? 我如何将以下bash行转换为perl? Could I run the system() command, or is there a better way?
我可以运行system()命令,还是有更好的方法? I'm looking for perl to print out access per day from my apache access_log file.
我正在寻找perl,以便每天从我的apache access_log文件中打印出访问权限。
In bash: 在bash中:
awk '{print $4}' /etc/httpd/logs/access_log | cut -d: -f1 | uniq -c
Prints the following: 打印以下内容:
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
Switches: 开关:
-l
automatically appends a newline to the print statement. -l
自动将换行符附加到print语句。 -l
also removes the newlines from lines read by -n
(and -p
). -l
还从-n
(和-p
)读取的行中删除换行符。 -a
splits the line on whitespace into the array @F
. -a
将空白行分割为数组@F
。 -n
loops over the lines of the input but does not print each line. -n
循环输入的行,但不打印每行。 -e
execute the given script body. -e
执行给定的脚本主体。 您的原始命令已翻译为Perl一线式:
perl -lane '($k) = $F[3] =~ /^(.*?):/; $h{$k}++ }{ print "$h{$_}\t$_" for keys %h' /etc/httpd/logs/access_log
You can change all your commands to one from: 您可以从以下位置将所有命令更改为一个:
awk '{print $4}' /etc/httpd/logs/access_log | cut -d: -f1 | uniq -c
to 至
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.