Skip to content

Instantly share code, notes, and snippets.

@inertialbit
Created October 24, 2012 19:18
Show Gist options
  • Save inertialbit/3948222 to your computer and use it in GitHub Desktop.
Save inertialbit/3948222 to your computer and use it in GitHub Desktop.
remove blank lines from a file
##
#
# Remove blank lines from a file.
#
# Usage:
#
# ruby vacuum.rb /path/to/file
#
module Vacuum
def self.run(filepath)
filename, filedir = File.basename(filepath), File.dirname(File.expand_path(filepath))
# Save output to new file with 'clean-' prepended to name.
file = File.open(File.join(filedir, "clean-#{filename}"), "w")
# Read source file from path & copy non-blank lines to new file.
File.foreach(filepath) { |line| file.print line if line.gsub(/\s/,'').size > 0 }
file.close
end
end
Vacuum.run(ARGV[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment