Count file lines with shell or php

MaxMind GeoIP2 City csv file is too large and Excel cannot open it and it crashes all editors.
So using shell, is it possible to cound how many rows/records this have? I know “cat” command but how to count the lines of csv? or how possible to know the lines with a separate php code?

what file size are we talking about?

around 180 Mb!

Does your system have a version of the “wc” command that will count words, lines etc?

You could explode by \n and then foreach and cont the loops.

I would recommend you not do this on a live host server, but for localhost it could work.

I recently wrote a script to work with a largish CSV file and had problems.

My work-around was to use ini_set() to throw more memory and time at it.

The CSV was 32 MB not 180 MB and I only tweaked up the value until it worked.
You will likely need “no limit” (and some patience while waiting for it to finish)

ini_set('memory_limit', '512M'); // 4x default
//ini_set('memory_limit', -1); // no limit

I think for a 180 MB file I would add a conditional to the while() to break out of the read() and write() new CSV files of smaller size

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.