首页 > 代码库 > 用sed删除空行

用sed删除空行

用sed删除空行

我的代码如下:
class Song

    def initialize(name)
        @name = name
    end

    def tell
        puts @name
    end

end

class ZhouSong < Song

    def initialize(name,artist)
        super(name)
        @artist = artist
    end

    def tell
        super
        puts @artist
    end

    def name=(newName)
        @name = newName
    end
    
    attr_writer :artist

end


s = Song.new("song")
s.tell


zs = ZhouSong.new("zhousong","zhoujielun")
zs.tell
zs.name = "name : new zhou song"
zs.tell

zs.artist = "artist : zhoujielun "
zs.tell

我希望删除所有的空行,可以用sed来实现
文件名是a.rb
more a.rb | sed "/^\s*$/d"
结果如下:
class Song
        def initialize(name)
                @name = name
        end
        def tell
                puts @name
        end
end
class ZhouSong < Song
        def initialize(name,artist)
                super(name)
                @artist = artist
        end
        def tell
                super
                puts @artist
        end
        def name=(newName)
                @name = newName
        end
        attr_writer :artist
end
s = Song.new("song")
s.tell
zs = ZhouSong.new("zhousong","zhoujielun")
zs.tell
zs.name = "name : new zhou song"
zs.tell
zs.artist = "artist : zhoujielun "
zs.tell

如果您想直接修改文件,那么可以用
sed -i "/^\s*$/d" a.rb

用sed删除空行