I’m trying to figure out how many lines exist in a CSV I’m about to iterate over so I can monitor progress.
From the command line, this gives the correct value:
cat /path/to/CA_MA.csv | perl -p -i -e "s/^M/\n/g" | wc -l
However, using shell_exec(), I get nothing back:
trim( shell_exec( "cat /path/to/CA_MA.csv | perl -p -i -e 's/^M/\n/g' | wc -l" ) )
I’ve tried both STDOUT and STDERR (... 2>&1). and I’ve tried this from my script from the interactive console (php -a).
Count lines in a file using PHP
It even works on files with “DOS” line endings.