Skip to content

Instantly share code, notes, and snippets.

@quwubin
Created June 8, 2012 01:31
Show Gist options
  • Save quwubin/2892871 to your computer and use it in GitHub Desktop.
Save quwubin/2892871 to your computer and use it in GitHub Desktop.
Ruby: Remove blank lines and leading ^M characters
#!/usr/bin/env ruby
#
# Wubin Qu <quwubin@gmail.com>
#
if ARGV.size != 1
$stderr.puts "
Remove blank lines and leading ^M characters
Usage:
#{$0} file_name
Author: Wubin Qu <quwubin@gmail.com>
"
exit
end
File.open(ARGV[0]).each_line do |line|
# Remove ^M when copy files from Windows
line.gsub!(/\r\n?/, "\n")
# Remove blank line
line.sub!(/^\n/, "")
next if line.size == 0
$stdout.puts line
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment