All Projects → tobimensch → Termsql

tobimensch / Termsql

Licence: other
Convert text from a file or from stdin into SQL table and query it instantly. Uses sqlite as backend. The idea is to make SQL into a tool on the command line or in scripts.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Termsql

Walkable
A Clojure(script) SQL library for building APIs: Datomic® (GraphQL-ish) pull syntax, data driven configuration, dynamic filtering with relations in mind
Stars: ✭ 384 (+66.96%)
Mutual labels:  sql, query, sqlite
Better Sqlite3
The fastest and simplest library for SQLite3 in Node.js.
Stars: ✭ 2,778 (+1107.83%)
Mutual labels:  sql, sqlite
Trilogy
TypeScript SQLite layer with support for both native C++ & pure JavaScript drivers.
Stars: ✭ 195 (-15.22%)
Mutual labels:  sql, sqlite
Migrate
Database migrations. CLI and Golang library.
Stars: ✭ 2,315 (+906.52%)
Mutual labels:  sql, sqlite
Nut
Advanced, Powerful and easy to use ORM for Qt
Stars: ✭ 181 (-21.3%)
Mutual labels:  sql, sqlite
Geopackage Js
GeoPackage JavaScript Library
Stars: ✭ 191 (-16.96%)
Mutual labels:  sql, sqlite
Askgit
Query git repositories with SQL. Generate reports, perform status checks, analyze codebases. 🔍 📊
Stars: ✭ 2,707 (+1076.96%)
Mutual labels:  sql, sqlite
Sqldb Logger
A logger for Go SQL database driver without modify existing *sql.DB stdlib usage.
Stars: ✭ 160 (-30.43%)
Mutual labels:  sql, query
Cg Sql
CG/SQL is a compiler that converts a SQL Stored Procedure like language into C for SQLite. SQLite has no stored procedures of its own. CG/CQL can also generate other useful artifacts for testing and schema maintenance.
Stars: ✭ 210 (-8.7%)
Mutual labels:  sql, sqlite
Octosql
OctoSQL is a query tool that allows you to join, analyse and transform data from multiple databases and file formats using SQL.
Stars: ✭ 2,579 (+1021.3%)
Mutual labels:  sql, query
Grdbcombine
GRDB ❤️ Combine
Stars: ✭ 220 (-4.35%)
Mutual labels:  sql, sqlite
Linq2db
Linq to database provider.
Stars: ✭ 2,211 (+861.3%)
Mutual labels:  sql, sqlite
Rom Sql
SQL support for rom-rb
Stars: ✭ 169 (-26.52%)
Mutual labels:  sql, sqlite
Jquery.terminal
jQuery Terminal Emulator - JavaScript library for creating web-based terminals with custom commands
Stars: ✭ 2,623 (+1040.43%)
Mutual labels:  terminal, parse
Query Exporter
Export Prometheus metrics from SQL queries
Stars: ✭ 166 (-27.83%)
Mutual labels:  sql, query
Kuery
Strongly typed SQL in Kotlin
Stars: ✭ 199 (-13.48%)
Mutual labels:  sql, sqlite
Bigbash
A converter that generates a bash one-liner from an SQL Select query (no DB necessary)
Stars: ✭ 230 (+0%)
Mutual labels:  sql, query
Oreilly getting started with sql
Database files for the O'Reilly book "Getting Started with SQL: A hands on approach for beginners" http://goo.gl/z3zG54
Stars: ✭ 156 (-32.17%)
Mutual labels:  sql, sqlite
Fselect
Find files with SQL-like queries
Stars: ✭ 3,103 (+1249.13%)
Mutual labels:  sql, query
Tsql Parser
Library Written in C# For Parsing SQL Server T-SQL Scripts in .Net
Stars: ✭ 203 (-11.74%)
Mutual labels:  sql, parse

TERMSQL

Convert text from a file or from stdin into SQL table and query it instantly. Uses sqlite as backend. The idea is to make SQL into a tool on the command line or in scripts.

Install

Requirements:

Installing using pip:

pip3 install termsql

To install termsql open a terminal and run:

sudo python setup.py install

Termsql now supports shorter and more convenient sql statements given the sqlparse module from https://pypi.python.org/pypi/sqlparse/ version 0.1.15 or higher is installed.

termsql "select col0,col1 from tbl"
#is equal to:
termsql select col0,col1

termsql -m line -1 "select USER,COUNT(*) from tbl group by USER"
#is equal to:
termsql -m line -1 "select USER,COUNT(*) group by USER"

Therefore installing the sqlparse module (which is tiny anyway) from https://pypi.python.org/pypi/sqlparse/ or https://github.com/andialbrecht/sqlparse is highly recommended. At least if you want to save yourself some typing.

Learn more

always helpful is:

termsql --help

and also:

man termsql

Online manual:

http://tobimensch.github.io/termsql

So what can it do?

  • convert text/CSV files into sqlite database/table
  • work on stdin data on-the-fly
  • it can be used as swiss army knife kind of tool for extracting information from other processes that send their information to termsql via a pipe on the command line or in scripts
  • termsql can also pipe into another termsql of course
  • you can quickly sort and extract data
  • creates string/integer/float column types automatically
  • gives you the syntax and power of SQL on the command line

Examples

export LC_ALL=en_US; top -b | head | termsql -1 -H 6 "select [PID],[USER],[COMMAND],[%CPU] from tbl where [%CPU]>=25"

termsql doesn't recognize numbers like "25,3" as numbers, but as strings. export LC_ALL=en_US ensures that top outputs numbers that are easy for termsql/sqlite to digest (ie. "25.3"). -H 6 makes termsql disregard the first 6 lines. We select only the processes with more than 25% cpu usage and output their PID,USER,COMMAND and %CPU.

export DISPLAY=$(ps aux | termsql "select COL11 from tbl where COL10 like '%Xorg.bin%' limit 1")

set DISPLAY environment variable to what display X is running on right now, assuming that the X binary is called "Xorg.bin")

ls -lha /usr/bin/* | termsql -w -r 8 "select * from tbl order by COL8 desc"

-r 8 merges the filenames into the 8th column. Then "order by COL8 desc" sorts them in reverse order. Due to -w the output looks nice on the command line

ps aux | termsql -m line -1 "select USER,COUNT(*) from tbl group by USER"

counts the total number of processes that each user has running. -1 gets the column names from the first line, therefore we can use USER instead of COL0 in the SQL statement. "group by USER" groups the rows of with identical USER together and for that reason COUNT(*) returns the total number of rows (in this case processes) for each USER. -m line beautifies the output.

termsql -ei .config/Bitcoin/Bitcoin-Qt.conf -c key,value -d = -p = -x "update tbl set value='true' where key='fMinimizeToTray'"

Demonstrates how you can use termsql to edit simple config files with key/value pairs. -i loads the config file as input and -e makes sure this same file is written to instead of stdout. Simply speaking: enabling edit mode. -d = sets the delimiter for splitting the input to = and -p = sets the separator for the output to = again, so that the output format matches the input format. -x appends a ";select * from tbl" to the user defined query, so that we get everything back that we put in. The user query is an SQL update statement which sets the value part of the key value pair to true, where the key is fMinimizeToTray. -c key,value allows us to use these convenient names instead of COL0 (key) and COL1 (value). Note that this approach may not always be perfect, for example in .config/Bitcoin/Bitcoin-Qt.conf there's a section line "[General]" which gets changed to "[General]=" after termsql is done with editing. Which may or may not be a problem depending on the program that uses the config file. It could easily be fixed with a tool like sed though.

For detailed information about options and more examples see the Manual

Vs awk

  • awk can't create permanent sqlite databases
  • sqlite is efficient, fast and mature (not saying that awk isn't)
  • many people have worked with SQL before that aren't experts on awk
  • SQL tends to look very descriptive, which should make it easy to maintain code and fix bugs
  • termsql is designed from the start to be as intuitive as possible, awk doesn't really strike the author of termsql as intuitive (that's a personal opinion and not a fact).
  • termsql can output to CSV,HTML,insert,SQL dump,tcl,text,line,SQlite database

Contribute

You like termsql and want to help in some way? Then why don't you tell others about it and show them how they can use it. Post your termsql usecases and ideas on twitter, facebook, google+, your-favorite-social-media, your blog or website. Making termsql more popular is a great contribution.

Another possible way to contribute is to help with improvement or translation of the documentation.

It would also be great, if people create packages for their own distribution or file bugreports for their distribution, so that termsql will be available in their favorite (Linux-)distribution.

Of course contributing code/patches is also welcome, look at the roadmap below or at the issues tracker on github for ideas on what to do or come up with your own ideas. Forking termsql is a great way to start as well.

Want to contribute by reporting a problem/bug? Feel free to use the issue tracker.

Roadmap

  • it's not documented in the man page yet, but termsql supports shorter SQL statements, where "from tbl" and other repetitive text isn't required anymore.

    • "select col0,col1 where col1='foo'" => "select col0,col1 from tbl where col1='foo'"
      • this particular feature is now commited and working given that you have sqlparse module version 0.1.15 and up
    • "set foo='X' where bar='Y'" => "update tbl set foo='X' where bar='Y'"
      • DONE
    • "where col4 like '%sometext%' => "select * from tbl where col4 like '%sometext%'"
      • DONE
    • Saving users some typing.
    • Making commands more concise.
  • type SQL commands without quotes

    • termsql -i input.txt select col3

      • DONE
    • quotes around the whole statement will continue to be recommended because you'll have to escape special charactes in the shell like this:

      termsql -i input.txt select col3 from tbl where col0="'test spaces'" ; select col 1 from tbl

  • add more examples to manual

  • support user scripts to preprocess column input

    • this would allow users to use tools like sed or anything they can think of to modify certain columns BEFORE they're inserted into the database

      termsql -i input.csv -d ':' --pp 3 my_script.sh "select * from tbl where col5>=100"

    • my_script.sh would receive the column data, the row number and the col number as input

  • get input from users how to improve further / what features to adds

  • prepare source/make packages for different distributions and OSs

    • help/contributions welcome!
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].