Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.81 KB | None | 0 0
  1. # File Tail
  2. # By Ashok Menon
  3. # 11-11-2010
  4. # Given two files, and a percentage [x], will copy the last x% of the first file, into the second file
  5.  
  6. abort "Incorrect Number of Arguments.\nFormat: \"ruby file-tail.rb [first-file] [second-file] [percent=30]\"" if !([2,3].include? ARGV.length)
  7.  
  8. first_file_path = ARGV[0]; second_file_path = ARGV[1]
  9.  
  10. if ARGV.length == 3
  11.   percent = ( ARGV[2].to_f ) / 100.0
  12. else
  13.   percent = 0.3
  14. end
  15.  
  16. abort "First File Does Not Exist." if !(File.exists? first_file_path)
  17.  
  18. first_file = File.new first_file_path
  19. second_file = File.new (second_file_path), "w+"
  20.  
  21. file_lines = first_file.readlines
  22. line_no = file_lines.length
  23. tail_length = (line_no*percent).round
  24. upperbound = line_no - 1
  25. lowerbound = upperbound - tail_length
  26.  
  27. file_lines[lowerbound..upperbound].each do |line|
  28.   second_file.puts line
  29. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement