I've lately become a fan of awk. One of the recent tasks I needed to do was to take a couple columns from 1000+ line spreadsheets and insert them into database tables. What I did, was save the spreadsheet to csv format and then wrote a one line awk script to extract the desired columns and build an insert statement.

Here it is:

awk -F"," '$9!=""{ print "insert into table_name (col1, col2, col3, col4) values ("$7","$9",'\''text1'\'','\''text2'\'');"}' spreadsheet.csv > insert_statements.sql

The beginning statement $9!="" tells awk to only take rows where the ninth column is not empty.

Comments: