doSQL - Run SQL commands on tabular text files

Post on 27-Jul-2015

60 views 1 download

transcript

doSQLdoSQLa command line script to run SQL on text files.

@wishvamalli

a command line script to run SQL on text files.

@wishvamalli

I’m a data guy.I’m a data guy.

I spend most of my time on the command line, I spend most of my time on the command line, working with text data files.working with text data files.

I also love SQL.I also love SQL.

But the sad thing is that there is no simple way to But the sad thing is that there is no simple way to directly execute SQL on a text file...directly execute SQL on a text file...

... without manually importing the text to a ... without manually importing the text to a database...database...

and that’s boring!and that’s boring!

So i wrote doSQL.py.So i wrote doSQL.py.

>> python doSQL.py --one data.txt --sql “SELECT * from one”>> python doSQL.py --one data.txt --sql “SELECT * from one”

now you can do...now you can do...

and also pipe the data to another command.and also pipe the data to another command.

>> python doSQL.py --one data.txt --sql “SELECT * from one” | head>> python doSQL.py --one data.txt --sql “SELECT * from one” | head

doSQL simply takes your data file and adds it to a doSQL simply takes your data file and adds it to a SQLite3 database saved in memory.SQLite3 database saved in memory.

Then it will execute your SQL and print the results.Then it will execute your SQL and print the results.

So any standard SQL statement would work.So any standard SQL statement would work.Including joins! Including joins!

(use --two to specify the second file).(use --two to specify the second file).

And as SQLite comes with python all you need is a And as SQLite comes with python all you need is a basic python installation on any OS, and you are basic python installation on any OS, and you are

good to go!good to go!

Enjoy!Enjoy!The code can be found in githubThe code can be found in github

https://github.com/wishvamalli/doSQL