Skip to content

Instantly share code, notes, and snippets.

@rergw
Created September 17, 2018 13:49
Show Gist options
  • Save rergw/413b567d64352246c7cb401a60486492 to your computer and use it in GitHub Desktop.
Save rergw/413b567d64352246c7cb401a60486492 to your computer and use it in GitHub Desktop.
Converts categorized text into text for spreadsheets. Copies results to clipboard.
=begin
Converts categorized text into text for spreadsheets.
Copies results to clipboard.
Converts:
```
cat 1
entry 1
entry 2
entry 3
cat 2
entry 4
entry 5
entry 6
```
to:
```
entry 1\tcat1
entry 2\tcat1
entry 3\tcat1
entry 4\tcat2
entry 5\tcat2
entry 6\tcat2
```
And puts contents on clipboard. Needs `gem install clipboard`.
Usage:
ruby path_of_this_file.rb file_to_convert start_line end_line
=end
require 'clipboard'
file = ARGV[0]
from = ARGV[1]
to = ARGV[2]
# puts "File is #{file}"
# puts File.read file
text = File.read(file)
if !from.empty?
# puts "from: #{from}"
section = (from.to_i - 1)..(to.to_i - 1)
text = text.split("\n")[section].join("\n")
end
groups = text.split("\n\n")
# puts "groups: #{groups}"
result = groups.reduce("") do |m, group|
lines = group.split("\n")
category = lines[0]
entries = lines[1..-1]
# handle empty categories
entries = ['N/A'] unless entries[0]
m += entries.map{|e| "#{e}\t#{category}"}.join("\n") + "\n"
end
# puts "result: #{result}"
Clipboard.copy(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment