Bash - remove all lines beginning with 'P'

前端 未结 5 827
Happy的楠姐
Happy的楠姐 2020-12-09 12:43

I have a text file that\'s about 300KB in size. I want to remove all lines from this file that begin with the letter \"P\". This is what I\'ve been using:

&g         


        
5条回答
  •  暖寄归人
    2020-12-09 12:58

    Use start of line mark and quotes:

     cat file.txt | egrep -v '^P.*'
    

    P* means P zero or more times so together with -v gives you no lines

    ^P.* means start of line, then P, and any char zero or more times

    Quoting is needed to prevent shell expansion.

    This can be shortened to

    egrep -v ^P file.txt
    

    because .* is not needed, therefore quoting is not needed and egrep can read data from file.

    As we don't use extended regular expressions grep will also work fine

    grep -v ^P file.txt
    

    Finally

    grep -v ^P file.txt > new.txt
    

提交回复
热议问题