繁体   English   中英

转换bash行以在perl中使用

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM