[英]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.