Skip to content

Instantly share code, notes, and snippets.

@spraints
Created February 19, 2014 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spraints/9095912 to your computer and use it in GitHub Desktop.
Save spraints/9095912 to your computer and use it in GitHub Desktop.
#/ Usage: ruby count-line-endings.rb file1 file2 ...
#/
#/ Reports how many of each kind of line endings there are in the files.
ARGV.each do |file|
File.open(file, 'rb') do |f|
puts "#{file}:"
state = :none
dos = unix = mac = 0
f.read.each_char do |c|
case c
when "\r"
state = :r
when "\n"
if state == :r
dos += 1
else
unix += 1
end
state = :none
else
if state == :r
mac += 1
end
state = :none
end
end
puts " \\n: #{unix}" if unix > 0
puts " \\r\\n: #{dos}" if dos > 0
puts " \\r: #{mac}" if mac > 0
puts " total: #{unix + dos + mac}"
end
end
if ARGV.empty?
puts File.readlines(__FILE__).select { |line| line =~ /^#\// }.map { |line| line[3..-1] }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment