Deano
Deano

Reputation: 12190

awk 2 perl conversion

I'm trying to convert this awk function to perl, it parses an apache log file and print entries created in the past minute.

awk vDate=$(date -d'now-1 minutes' +[%d/%b/%Y:%H:%M:%S]) '$4 > Date '

I'm new to perl, and I'm now sure how I can entorplate system function $(date -d'now-1 ..etc) to perl.

Upvotes: 0

Views: 134

Answers (1)

choroba
choroba

Reputation: 241808

Use Time::Piece:

perl -MTime::Piece -ane '
    BEGIN { $now = time }
    print if Time::Piece->strptime($F[3], "[%d/%b/%Y:%H:%M:%S]")->epoch
          > $now - 60' access_log

You can also use $^T instead of $now, you can then delete the whole BEGIN block.

perl -MTime::Piece -ane '
    print if Time::Piece->strptime($F[3], "[%d/%b/%Y:%H:%M:%S]")->epoch
          > $^T - 60' access_log

Upvotes: 4

Related Questions