in reply to Re: Count number of lines?
in thread Count number of lines?

I believe Abigail scored the ultimate Perl one-liner to count lines in a file, with (modified to fit the current context)...

  perl -lpe '}{$_="$. users to date"' users.dat

A little more readable is ...

  perl -lne 'END { print "$. users to date" }' users.dat

    --k.


Replies are listed 'Best First'.
Re: Re: Re: Count number of lines?
by converter (Priest) on Jun 20, 2001 at 08:56 UTC

    For those of you who wondered, as I did, here's what perl -lpe '}{$_="$.users to date"' is doing:

    The -p option wraps your code in the following loop (see perlrun):

    LINE:
      while (<>) {
        ...             # your program goes here
      } continue {
        print or die "-p destination: $!\n";
      }
    

    The code supplied with the -e switch inserts a closing curly brace, creating an empty while BLOCK, followed by an opening curly brace, which makes a bare block after the while block.

    LINE:
      while (<>) { }
      { $_="$. users to date" }
      continue {
        print or die "-p destination: $!\n";
      }
    

    The while loop reads all the input records, the bare block assigns the string including the value of the input record counter to $_, and the continue block prints the string.