有时会需要使用 grep 做多关键词匹配,这里给出集中方法,简单记录: grep ‘word1\|word2\|word3’ /path/to/file ### Search all text files ### $ grep ‘word*‘ *.txt ### Search all python files for ‘wordA’ or ‘wordB’ ### $ grep ‘wordA*‘\‘’wordB’ *.py $ grep -E ‘word1|word2’ *.doc $ grep -e string1 -e string2 *.pl $

有时会需要使用 grep 做多关键词匹配,这里给出集中方法,简单记录:

1
2
3
4
5
6
7
8
9
10
11
grep 'word1\|word2\|word3' /path/to/file
### Search all text files ###
$ grep 'word*' *.txt
### Search all python files for 'wordA' or 'wordB' ###
$ grep 'wordA*'\''wordB' *.py
$ grep -E 'word1|word2' *.doc
$ grep -e string1 -e string2 *.pl
$ grep -E "word1|word2" *.c
### Show all the lines that do not match given pattern/words/strings ###
$ grep -v 'bar\|foo' /dir1/dir2/file1
$ grep -E -v 'pattern1|pattern2' /path/to/file

参考文献