-7

下午好,我为以下场景编写了一个 shell 脚本:

找到今天的所有文件并阅读这些文件并获得以 ~ 分隔的第六个字段

================
#!/usr/bin/ksh
DATE=`date | awk '{print $2 " " $3}'`
TIMESTAMP=`date +"%m%d%Y"`
for filename in `ls -ltr | grep -i "$DATE"| awk '{print $9}'`
do
  cat $filename | grep -i ^01 | awk -F'~' '{print $6}' >> /tmp/log.$TIMESTAMP
done
================

有人可以为此提供一个等效的perl。

4

2 回答 2

2

I think this does the trick, although my Korn shell is very rusty.

It differs by checking the actual modification time against the previous midnight instead of searching for the month and day in an ls listing.

use strict;
use warnings;

use File::Find 'find';
use Time::Piece 'localtime';

my $timestamp = localtime->strftime('%m%d%Y');
my $date = Time::Piece->strptime($timestamp, '%m%d%Y')->epoch;

open my $log, '>', "/tmp/log.$timestamp" or die "Unable to open log file: $!";

find(sub {
  return unless -f and (stat)[9] >= $date;
  open my $fh, '<', $_ or die qq{Unable to open "$_" for reading: $!};
  while (<$fh>) {
    chomp;
    next unless /^01/;
    print $log (split /~/)[5], "\n";
  }
}, '.');
于 2013-04-23T19:29:28.580 回答
-2
open KSH, "| /usr/bin/ksh";
print KSH <<'EOF';
DATE=`date | awk '{print $2 " " $3}'`
TIMESTAMP=`date +"%m%d%Y"`
for filename in `ls -ltr | grep -i "$DATE"| awk '{print $9}'`
do
  cat $filename | grep -i ^01 | awk -F'~' '{print $6}' >> /tmp/log.$TIMESTAMP
done
EOF
close KSH;

其他任何东西都不完全等同。

于 2013-04-23T20:19:14.713 回答