Skip to content

Instantly share code, notes, and snippets.

Last active December 24, 2021 18:11
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Full text search a single text file
#!/usr/bin/env ruby
# v2021-12-24.1
require 'sqlite3'
dbf = '/tmp/plan.db'
File.delete(dbf) if File.exists? dbf
db = dbf
db.results_as_hash = true
db.execute "CREATE VIRTUAL TABLE IF NOT EXISTS plan USING FTS5(contents,tokenize = porter)"
db.execute "INSERT INTO plan (contents) VALUES(?)",[0]).read
results = db.query "SELECT snippet(plan, 0,'[', ']', '...',32) as result FROM plan(?) ORDER BY rank ", ARGV.slice(1,ARGV.length).join(" ")
while r =
puts r['result']
Copy link

svandragt commented Dec 24, 2021

Setup: $ chmod +x fts.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment