Created
April 1, 2014 13:45
-
-
Save pi-chan/9914266 to your computer and use it in GitHub Desktop.
Using Ruby command line options by Arjan van der Gaag http://arjanvandergaag.nl/blog/using-ruby-command-line-options.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -w | |
# This tranforms input files that look like CSV and strips comments and | |
# filters out every line not about "Suriname". | |
# Define some basic variables that control how records and fields | |
# are defined. | |
input_record_separator = "\n" | |
field_separator = ',' | |
output_record_separator = "\n" | |
output_field_separator = ';' | |
filename = ARGV[0] | |
File.open(filename, 'r+') do |f| | |
# Read the entire contents of the file in question | |
# in an input array. | |
input = f.readlines(input_record_separator) | |
output = '' | |
# Loop over all the lines in the file with a counter | |
input.each_with_index do |last_read_line, i| | |
# Remove the ending newline from the line for easier | |
# processing. | |
last_read_line.chomp!(input_record_separator) | |
# Extract all fields in this record. | |
fields = last_read_line.split(field_separator) | |
# Only proceed for non-comment lines about Suriname | |
if fields[5] == 'Suriname' && !(last_read_line =~ /^# /) | |
# Write the output lines including the line number | |
# and combine fields using our custom separator | |
fields.unshift i | |
output << fields.join(output_field_separator) | |
output << output_record_separator | |
end | |
end | |
# Rewind back to the start of the file and replace all its | |
# contents with the content in `output`. | |
f.rewind | |
f.write output | |
f.flush | |
f.truncate(f.pos) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment