bash - SQLITE3 Import .csv via Mac Terminal and Skip the First Row -


i have thousands of .csv files i'd import sqlite3 database. each .csv file has labels on first row.

my idea run loop on file names , import them using .import command:

    sqlite3 options.db ".mode csv data" ".import $f data" 

the problem import first row contains labels.

question: how .import .csv files without importing first row of each file?

thanks helping! :d

assume file1.csv looks this:

file1,line1 file1,line2 file1,line3 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 1,2,3,4,5,6 

and file2.csv looks this:

file2,line1 file2,line2 file2,line3 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 1,2,3,4,5,6 

then can make awk print csv files whilst skipping first line (where fnr, i.e. file line number, 1) this:

awk -f, 'fnr==1{next} 1' *csv 

output

file1,line2 file1,line3 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 1,2,3,4,5,6 file2,line2 file2,line3 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 1,2,3,4,5,6 

then can check number of fields present on each line, , if not 23, add many commas necessary make 23:

awk -f, 'fnr==1{next} nf!=23{$0=$0 substr(",,,,,,,,,,,,,,,,,,,,,,",1,23-nf)}1' *csv 

output

file1,line2,,,,,,,,,,,,,,,,,,,,, file1,line3,,,,,,,,,,,,,,,,,,,,, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,,,,, 1,2,3,4,5,6,,,,,,,,,,,,,,,,, file2,line2,,,,,,,,,,,,,,,,,,,,, file2,line3,,,,,,,,,,,,,,,,,,,,, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,,,,, 1,2,3,4,5,6,,,,,,,,,,,,,,,,, 

so, actual complete solution, this:

awk -f, 'fnr==1{next} nf!=23{$0=$0 substr(",,,,,,,,,,,,,,,,,,,,,,",1,23-nf)}1' *csv > newbigfile.csv 

and import newbigfile.csv sqlite.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -