Skip to content

Instantly share code, notes, and snippets.

@ostephens
Created June 4, 2015 16:42
Show Gist options
  • Save ostephens/75f53bcf65821e72dedb to your computer and use it in GitHub Desktop.
Save ostephens/75f53bcf65821e72dedb to your computer and use it in GitHub Desktop.
Extract URL from 856$u in MARC (Ruby)
!/usr/bin/ruby
require 'rubygems'
require 'marc'
require 'optparse'
require 'CSV'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: 001-856.rb [options]"
opts.on("-f", "--format [FORMAT]","Specify format of files to be converted. Currently supported 'marcxml' and 'marc'") do |f|
options[:format] = f
end
opts.on("-s", "--sourcefile [SOURCEFILE]","Specify source file") do |s|
options[:sourcefile] = s
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end.parse!
sourcefile = options[:sourcefile]
format = options[:format]
# reading records from a batch file
if(format == "marcxml")
reader = MARC::XMLReader.new(sourcefile)
elsif(format == "marc")
reader = MARC::Reader.new(sourcefile, :external_encoding => "UTF-8", :invalid => :replace)
else
puts "Not a recognised format. Currently accept 'marcxml' or 'marc'"
exit
end
for record in reader
record.each_by_tag('856') {|field|
puts record['001'].value.to_s + "," + field['u'].to_s
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment