Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

getting line count in cmd.exe

by chuckd (Scribe)
on Sep 15, 2008 at 01:35 UTC ( #711353=perlquestion: print w/ replies, xml ) Need Help??
chuckd has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone know how to get a line count in cmd.exe? I can use cygwin's 'wc -l filename', but then I have to have cygwin installed (I'm using a Windows machine). I know I can open the file in Perl and read line by line to the end and take a count but that takes appx. 3 or so lines. I'm trying to do it with as few lines as possible. Thanks.

Comment on getting line count in cmd.exe
Replies are listed 'Best First'.
Re: getting line count in cmd.exe
by jwkrahn (Monsignor) on Sep 15, 2008 at 01:42 UTC
      Just FTR, appears (testing with 5.8 and w2k against .pl and (ASCII text) .txt files as the targets) that this produces an accurate count on the .pl files, but not so with those named *.txt.

      First suspicion was that some doubled \n instances might be the problem, but further runs aginst .txt files without blank lines disabused me of that: perl -pe"}{$\=$." f:\_wo\(various).txt, consistently undercounted the lines.

      Is it just late on a hot, sticky and perhaps "stupidity-inducing" night, or do others see this too?

        There's no reason why that should be. Why would <> returns something different based on the input's extension.

        >perl -MO=Deparse -pe"}{$\=$." LINE: while (defined($_ = <ARGV>)) { (); } { $\ = $.; } continue { die "-p destination: $!\n" unless print $_; } -e syntax OK

        And I don't see it happening

        >perl -pe"}{$\=$." 56 >copy test.txt 1 file(s) copied. >perl -pe"}{$\=$." test.txt 56

        Provide a hexdump of your text file if you want to look into this further.

Re: getting line count in cmd.exe
by poolpi (Hermit) on Sep 15, 2008 at 12:15 UTC

    See Perl Cookbook, chapter 8.2 for more informations

    perl -ne 'END{print $.}' file


    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://711353]
Approved by ww
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2015-10-10 09:50 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (255 votes), past polls