Created
October 24, 2012 19:18
-
-
Save inertialbit/3948222 to your computer and use it in GitHub Desktop.
remove blank lines from a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# | |
# 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