Remove first line of CSV file

On 03-15-2015 articles, code

Command Line Options

If you are comfortable with the command line, or even if you aren’t and just need to quickly remove the first line of any file, here are a few one liners!

sed 1d readfile.csv > outputfile.csv
awk 'NR>1' readfile.csv > outputfile.csv
tail -n +2 readfile.csv > tmp.csv && mv tmp.csv outputfile.csv

Original Post*

Recently I needed to run  a script to remove the first line of a CSV file and was getting stumped (mostly on the encoding part)! Finally after some testing and trouble shooting and a little Google help I found a solution. And I am hoping this post will find it’s way to others with similar needs. And credit to stackoverflow.

This solution is in PHP.

Firs you need to read the document, store the data in memory and then manipulate it and finally rewrite the document. Code is below. This code edits a file on your local machine and of course you could incorporate it to a server file, but it is strictly for editing a file and not used for any database work.

//Read File
$file = fopen('filewithfullpath.csv', 'r');
while (($data_tmp = fgetcsv($file, 1000, ",")) !== FALSE) {
$data[] = $data_tmp;

//Remove first line

//Write File
$file = fopen('filewithfullpath.csv', 'w');
//Set UT-8
fprintf($file, chr(0xEF).chr(0xBB).chr(0xBF));
foreach($data as $d){
fputcsv($file, $d, ',');

Some trouble areas I had:

I also needed the final csv file to be in a UT-8 encoding, fputcsv function doesn’t do that for you so I found a nifty bit of code online that help achieve that (see Set UT-8 comment in code above) fprintf prints a encoding line before fputcsv writes the new data as csv.

The second thing I came across is I was pulling up the newly edited (remove first line from csv) document inside of notepad on a Windows machine and it wasn’t showing me any line breaks, and so I thought my code wasn’t working and turns out it was working I just needed to view it in a different editor, I pulled it up in my code editor and sure enough there were line breaks as it should of been. Something about notepad not reading CF breaks…something along those lines.