Skip to content

Instantly share code, notes, and snippets.

@svandragt
Last active Dec 24, 2021
Embed
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 = SQLite3::Database.open 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(?)", File.open(ARGV[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 = results.next
puts r['result']
end
@svandragt
Copy link
Author

svandragt commented Dec 24, 2021

Setup: $ chmod +x fts.rb
image

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