简体   繁体   中英

Convert bash line to use in perl

How would I go about converting the following bash line into perl? Could I run the system() command, or is there a better way? I'm looking for perl to print out access per day from my apache access_log file.

In 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 also removes the newlines from lines read by -n (and -p ).
  • -a splits the line on whitespace into the array @F .
  • -n loops over the lines of the input but does not print each line.
  • -e execute the given script body.

您的原始命令已翻译为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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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