Neatest way to remove linebreaks in Perl

前端 未结 7 1180
孤街浪徒
孤街浪徒 2020-12-23 09:16

I\'m maintaining a script that can get its input from various sources, and works on it per line. Depending on the actual source used, linebreaks might be Unix-style, Windows

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-23 09:47

    In your example, you can just go:

    chomp(@lines);
    

    Or:

    $_=join("", @lines);
    s/[\r\n]+//g;
    

    Or:

    @lines = split /[\r\n]+/, join("", @lines);
    

    Using these directly on a file:

    perl -e '$_=join("",<>); s/[\r\n]+//g; print' );print @a' 

提交回复
热议问题