How to count the number of lines in a large CSV file using Perl?

I need to use Perl in a Windows environment at work, and I need to find out the number of lines that a large csv file contains (about 1.4Gb). Any idea how to do this with minimal resources?

thanks

PS This should be done in a Perl script, and we are not allowed to install any new modules on the system.

+3
source share
6 answers

? , , . , , Perl FAQ. CSV, Text:: xSV.

+14

, perl.

; wc.exe

Windows, unix.

http://unxutils.sourceforge.net/

:

PS D:\> wc test.pl
     12      26     271 test.pl
PS D:\>

12 == , 26 == , 271 == .

perl;

D:\>perl -lne "END{print $.;}" < test.pl
12
+9
perl -lne "END { print $. }" myfile.csv

, , .

+4

:

  • .
  • , .
  • .

    perl -ne 'BEGIN{$re=qr/^[^"]*(?:"[^"]*"[^"]*)*?"[^"]*$/;}END{print"Count: $t\n";}$t++ unless /$re/../$re/'
    

:

</" > EDIT:. , :

perl -ne "BEGIN{$re=qr/^[^\"]*(?:\"[^\"]*\"[^\"]*)*?\"[^\"]*$/;}END{print qq/Count: $t\n/;};$t++ unless $pq and $pq = /$re/../$re/;"

, Broken OS && exec , , !! , , .

+3
source

Upvote for edg's answer, another option is to install cygwin to get wc and a bunch of other useful utilities on Windows.

0
source

I was an idiot, an easy way to do this in a script:

open $extract, "<${extractFileName}" or die ("Cannot read row count of $extractFileName");
$rowCount=0;    
while (<$extract>)
{
    $rowCount=$rowCount+1;
}

close($extract);
-1
source

Source: https://habr.com/ru/post/1706469/


All Articles