0

How would I go about deleting a specific number of rows from a large csv file? ie, I am looking to delete the first 100 rows from a file containing over 200,000 rows. Just curious to see if there is another way to handle things other than completely re-writing the file to a temp file and back to overwrite the original file.

Thanks in advance for any help/suggestions.

  • 1
    Open the file, skip the first 100 lines, write the remaining using this :https://stackoverflow.com/questions/7405828/streamwriter-rewrite-the-file-or-append-to-the-file – Danimal May 24 '18 at 20:35

1 Answers1

0

Does it need to be in php?

You can use the unix command tail to print every line in a file starting from the nth line:

tail -n +101 file.csv > trimmed.csv

Or using PHP:

$lines = file('file.csv'); // read file as array of lines $trimmed = array_slice($lines, 100); // slice array at index file_put_contents('trimmed.csv', implode($trimmed)); // implode and write to file

Michael Ozeryansky
  • 7,204
  • 4
  • 49
  • 61