How can I exclude one word with grep?

后端 未结 9 2435
孤独总比滥情好
孤独总比滥情好 2020-12-12 09:01

I need something like:

grep ^\"unwanted_word\"XXXXXXXX
9条回答
  •  攒了一身酷
    2020-12-12 09:29

    The right solution is to use grep -v "word" file, with its awk equivalent:

    awk '!/word/' file
    

    However, if you happen to have a more complex situation in which you want, say, XXX to appear and YYY not to appear, then awk comes handy instead of piping several greps:

    awk '/XXX/ && !/YYY/' file
    #    ^^^^^    ^^^^^^
    # I want it      |
    #            I don't want it
    

    You can even say something more complex. For example: I want those lines containing either XXX or YYY, but not ZZZ:

    awk '(/XXX/ || /YYY/) && !/ZZZ/' file
    

    etc.

提交回复
热议问题