Skip to content

Instantly share code, notes, and snippets.

@masao
Last active January 16, 2020 11:48
Show Gist options
  • Save masao/eff3ee10b7b7a4dad656e8ac8d46421e to your computer and use it in GitHub Desktop.
Save masao/eff3ee10b7b7a4dad656e8ac8d46421e to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
data = {}
bibdata = {}
holdings = {}
ARGF.each do |line|
line.chomp!
line.scan(/(?:\w+=(?:.*?)|<\/?\w+\>) /).each do |element|
case element
when /\A(\w+)=(.+)\Z/
key = $1.downcase.intern
val = $2.chop
data[key] = val
case key
when :id
data[:ncid] = val
when :trd
data[:trd_title], data[:trd_author], = data[:trd].split(/ \/ /)
end
end
end
case data[:_dbname_]
when "BOOK"
bibdata[data[:ncid]] = data
when "BHOLD"
holdings[data[:bid]] ||= []
holdings[data[:bid]] << data
end
data = {}
end
bibdata.sort_by{|k,v| k }.each do |ncid, v|
if holdings[ncid]
bib = [:ncid, :trd_title, :trd_author, :ed, :publ, :pubdt, :year1, :physp, :physs, :trr ].map{|e| v[e] }.join("\t")
holdings[ncid].each do |holding|
puts [ bib, holding[:vol], holding[:cln], holding[:rgtn] ].join("\t")
end
else
puts [:ncid, :trd_title, :trd_author, :ed, :publ, :pubdt, :year1, :physp, :physs, :trr ].map{|e| v[e] }.join("\t")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment