将PERL输出到.txt文件和屏幕

时间:2013-11-04 17:01:41

标签: perl text

这就是我所拥有的。

#!/usr/bin/perl

$numbertotal = 0;
$filecount = 0;

open my $thisfile, '<', "files.txt" or die $!;

while (<$thisfile>) {
    ($thisdate, $thistime, $thisampm, $thissize, $thisname) = split;

    $numbertotal += $thissize;
    $filecount += 1;
    printf "%10d     %-25.25s\n", $thissize, $thisname;
}

$averagefilesize = $numbertotal / $filecount;

print "Total files:  ",$filecount,"      Average file size:  ",$averagefilesize," bytes\n";

我想将2条不同的打印行发送到另一个将由代码创建的文件中。我知道它会使用'&lt;'操作,但我有一个问题搞清楚。

感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:1)

要写入文件,请使用'>'打开文件:

open my $thisfile, '<', "files.txt" or die $!;
open my $thatfile, '>', 'output.txt' or die $!;

while (<$thisfile>) {
    ($thisdate, $thistime, $thisampm, $thissize, $thisname) = split;

    $numbertotal += $thissize;
    $filecount += 1;
    printf $thatfile "%10d     %-25.25s\n", $thissize, $thisname;
}
close $thatfile;
close $thisfile;