Remove blank lines with grep

后端 未结 15 2037
后悔当初
后悔当初 2020-12-22 17:51

I tried grep -v \'^$\' in Linux and that didn\'t work. This file came from a Windows file system.

15条回答
  •  粉色の甜心
    2020-12-22 18:13

    If you have sequences of multiple blank lines in a row, and would like only one blank line per sequence, try

    grep -v "unwantedThing" foo.txt | cat -s
    

    cat -s suppresses repeated empty output lines.

    Your output would go from

    match1
    
    
    
    match2
    

    to

    match1
    
    match2
    

    The three blank lines in the original output would be compressed or "squeezed" into one blank line.

提交回复
热议问题