Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # File Tail
- # By Ashok Menon
- # 11-11-2010
- # Given two files, and a percentage [x], will copy the last x% of the first file, into the second file
- abort "Incorrect Number of Arguments.\nFormat: \"ruby file-tail.rb [first-file] [second-file] [percent=30]\"" if !([2,3].include? ARGV.length)
- first_file_path = ARGV[0]; second_file_path = ARGV[1]
- if ARGV.length == 3
- percent = ( ARGV[2].to_f ) / 100.0
- else
- percent = 0.3
- end
- abort "First File Does Not Exist." if !(File.exists? first_file_path)
- first_file = File.new first_file_path
- second_file = File.new (second_file_path), "w+"
- file_lines = first_file.readlines
- line_no = file_lines.length
- tail_length = (line_no*percent).round
- upperbound = line_no - 1
- lowerbound = upperbound - tail_length
- file_lines[lowerbound..upperbound].each do |line|
- second_file.puts line
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement