我得到下你的編輯介紹的情況下輸出:
$ echo "aaaproc1bbb" | grep -Eo 'proc1|proc2'
proc1
$ echo $?
0
$ echo "aaabbb" | grep -Eo 'proc1|proc2'
$ echo $?
1
退出代碼顯示如果沒有匹配。
您也可能會發現這些選項grep
有用(-L
可能是特定於GNU的grep):
-c, --count
Suppress normal output; instead print a count of matching lines
for each input file. With the -v, --invert-match option (see
below), count non-matching lines. (-c is specified by POSIX.)
-L, --files-without-match
Suppress normal output; instead print the name of each input
file from which no output would normally have been printed. The
scanning will stop on the first match.
-l, --files-with-matches
Suppress normal output; instead print the name of each input
file from which output would normally have been printed. The
scanning will stop on the first match. (-l is specified by
POSIX.)
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit
immediately with zero status if any match is found, even if an
error was detected. Also see the -s or --no-messages option.
(-q is specified by POSIX.)
對不起,你引述man
頁面,但有時它可以幫助篩選事情有點。
編輯:
對於(不區分大小寫)的文件名列表不包含任何的程序:
grep -EiL 'proc1|proc2' *
對於包含的任何程序的文件名列表(區分不區分大小寫):
grep -Eil 'proc1|proc2' *
列出文件並顯示匹配(不區分大小寫):
grep -Eio 'proc1|proc2' *
什麼版本的'grep',什麼shell(和版本)和什麼分佈?你是否使用'-v'作爲'grep'的選項之一?看到我編輯的答案。 – 2009-11-18 15:27:45