Bash - remove all lines beginning with 'P'

前端 未结 5 836
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 13:20

    With awk:

    awk '!/^P/' file.txt
    

    Explanation

    1. The condition starts with an ! (negation), that negates the following pattern ;
      • /^P/ means "match all lines starting with a capital P",
    2. So, the pattern is negated to "ignore lines starting with a capital P".
    3. Finally, it leverage awk's behavior when { … } (action block) is missing, that is to print the record validating the condition.

    So, to rephrase, it ignores lines starting with a capital P and print everything else.

    Note

    sed is line oriented and awk column oriented. For your case you should use the first one, see Edouard Lopez's reponse.

提交回复
热议问题