On Sat, May 05, 2001 at 12:12:08AM -0400, Jeff Pinyan wrote:
: On May 4, Brett W. McCoy said:
:
: >On Fri, 4 May 2001, Susan Richter wrote:
: >
: >> Can someone tell me what command I can use to go to machines and count the
: >> lines on a certain file? I have log files for every machines (20 all
: >> together). Each log file has lines that would equate to the files that it
: >> found. I need to know how many files are found on all the machines. I am
: >> using Perl 5 on NT.
: >
: >my $counter = 0;
: >while(<FILE>) { $counter++; }
:
: In fact, no counter variable is needed. Perl supplies the $. variable,
: which holds the line number of the most recently read line.
:
: 1 while <FILE>;
: print $.;
Since we have been talking quite a bit about Perl on the command line,
including the -p and -n switches, I'll throw in the next lesson in
that series:
perl -nle '} print $.; {' file
This will print a line count for you. But, how does it work? I'll
show you the ouput of B::Deparse to give you an idea. First, a
standard:
[cwest@stupid cwest]$ perl -MO=Deparse -nle 'print $.' file
LINE: while (defined($_ = <ARGV>)) {
chomp $_;
print $.;
}
-e syntax OK
[cwest@stupid cwest]$
When run as "perl -nle 'print $.' file", each line number would be
printed to the screen in order. Now let's Deparse my original to see
what happens:
[cwest@stupid cwest]$ perl -MO=Deparse -nle'} print $.; {' file
LINE: while (defined($_ = <ARGV>)) {
chomp $_;
}
print $.;
{;};
-e syntax OK
[cwest@stupid cwest]$
As you can see, I added the '}' at the beginning of my program which
finished the while loop, then I "print $." which contians the last
line number because we already looped over the file. Then, I provide
a '{' to close the block at the end ( that would have been the end of
the while loop ).
Read more in:
perldoc perlrun
perldoc perlvar ( for $. )
Enjoy!
Casey West
--
Shooting yourself in the foot with PL/I
You consume all available system resources, including all the offline
bullets. The Data Processing & Payroll Department doubles its size,
triples its budget, acquires four new mainframes and drops the
original one on your foot.