t = File.read("x")[/------$\n(.*)--$/m, 1]i = {}t.split(/\n\n+/).each { |entry| a, *ms = entry.split(/\s{2,}|\t/) ms.each { |m| (i[m] ||= []) << a }}i.keys.sort.each { |m| puts m i[m].each { |a| puts "\t#{a}" }}