|
Versions Of This Snippet::
Download a raw-text version of this code by clicking on "Download Version"
Latest Snippet Version: :0.1
#!/usr/bin/env ruby -w
def usage
puts "usage: #{File.basename( __FILE__ )} file_to_split regular_expression_identifying_lines_to_split_at"
end
if ARGV.length != 2 then
usage
exit( 1 )
else
@file_to_split = ARGV[0]
@expression_identifying_lines_to_split_at = ARGV[1]
end
regular_expression_identifying_lines_to_split_at = Regexp.new( @expression_identifying_lines_to_split_at )
output_file = nil
output_file_name_counter = 0
File.open( @file_to_split ) { |file|
file.each { |line|
# If the current line matches the pattern of the delimiting line, create a new output file.
create_new_output_file = ( line =~ regular_expression_identifying_lines_to_split_at )
if create_new_output_file then
# If there is currently an output file open, close it.
if ! output_file.nil? then
output_file.close
end
# Increment the filename extension counter and create the new output file.
output_file_name_counter += 1
output_file = File.open( "#{@file_to_split}.#{output_file_name_counter}", 'w' )
end
output_file.print( line )
}
}
Submit a new versionYou can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..
|
||||||||||||||||||||||||||
