RU Ahmed
RU Ahmed

Reputation: 558

How to ignore the last five lines of a CSV file While using "load data infile"

The code below ignores the first five lines BUT I want to ignore last five lines of my CSV file

$query = <<<eof
LOAD DATA LOCAL INFILE '$fname'
 INTO TABLE rawdata_01
 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
 LINES TERMINATED AT '\n'
 IGNORE 5 LINES
(SequenceNumber,CDRMajorVerision)
 eof;

Upvotes: 1

Views: 3065

Answers (1)

Paul
Paul

Reputation: 27423

mySQL doesn't have support for ignoring ending lines.

From The MySQL 5.1 documentation:

The IGNORE number LINES option can be used to ignore lines at the start of the file. For example, you can use IGNORE 1 LINES to skip over an initial header line containing column names.

Instead, on a Linux system, you could go to a terminal window and use the command head to make a new file that omits the last 5 lines as follows:

head --lines=-5 oldfile > newfile

See: man page for head

Your other alternatives, (e.g. for Windows), are loading the entire file and deleting the last 5 lines somehow, and writing a script to do what head does in your language of your choice.

Upvotes: 1

Related Questions