Larry
Larry

Reputation: 93

Output PERL to a .txt file and to screen

Here's what I have.

#!/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";

I would like to take the 2 different print lines and send them to another file that will be created by the code. I know that it will use the '<' operation but I'm having an issue figuring it out.

Any help is appreciated. Thanks.

Upvotes: 0

Views: 266

Answers (1)

tripleee
tripleee

Reputation: 189830

To write to a file, you open it with '>':

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;

Upvotes: 1

Related Questions