\cat software
Sometimes I need to convert MS Access data or Excel to a PostgreSQL table. However, their export filters do not satisfy all my needs, so I made a small perl script to convert a CSV-like text file to an SQL file.
It reads a file with a separator, TAB by default, tries to determine data type for each field and its maximum width, then it prints the table definition and INSERT queries for PostgreSQL. You may want to review the CREATE definition for data type checking before feeding the output to a database. Simply by modifying data type in the definition, it can be easily converted to SQL for any other DBMS engines.
Here is a sample file (sample data.txt):
id sample type (AB, CC) location (km, km) 1 AB 23, 3 2 AB 1, 2 3 CC 40, 40
Running txt2sql "sample data.txt" prints the following:
create table "sample data" ( "id" int, "sample type (AB, CC)" varchar(2), "location (km, km)" varchar(6) ); insert into "sample data" values('1', 'AB', '23, 3'); insert into "sample data" values('2', 'AB', '1, 2'); insert into "sample data" values('3', 'CC', '40, 40');