How can I exclude one word with grep?

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

I need something like:

grep ^\"unwanted_word\"XXXXXXXX
9条回答
  •  盖世英雄少女心
    2020-12-12 09:26

    grep provides '-v' or '--invert-match' option to select non-matching lines.

    e.g.

    grep -v 'unwanted_pattern' file_name
    

    This will output all the lines from file file_name, which does not have 'unwanted_pattern'.

    If you are searching the pattern in multiple files inside a folder, you can use the recursive search option as follows

    grep -r 'wanted_pattern' * | grep -v 'unwanted_pattern'
    

    Here grep will try to list all the occurrences of 'wanted_pattern' in all the files from within currently directory and pass it to second grep to filter out the 'unwanted_pattern'. '|' - pipe will tell shell to connect the standard output of left program (grep -r 'wanted_pattern' *) to standard input of right program (grep -v 'unwanted_pattern').

提交回复
热议问题