Skip to content

Instantly share code, notes, and snippets.

@quwubin
Created June 8, 2012 01:31

Revisions

  1. quwubin created this gist Jun 8, 2012.
    26 changes: 26 additions & 0 deletions remove_blank_line.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    #!/usr/bin/env ruby
    #
    # Wubin Qu <[email protected]>
    #

    if ARGV.size != 1
    $stderr.puts "
    Remove blank lines and leading ^M characters
    Usage:
    #{$0} file_name
    Author: Wubin Qu <[email protected]>
    "
    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