Files
查找包含一個字元串但不包含另一個字元串的文件
我在一個有很多文件的
.txt
文件夾中,我想找到所有包含stringA
但不包含的文件stringB
(它們不一定在同一行)。有誰知道如何做到這一點?
只要您的文件名不包含空格、製表符、換行符或萬用字元,並且如果您
grep
支持該-L
選項,您可以按以下方式進行操作:$ cat file1 stringA stringC $ cat file2 stringA stringB $ grep -L stringB $(grep -l stringA file?) file1
在
grep
子shell 中執行$()
,將列印所有包含stringA
. 此文件列表是主grep
命令的輸入,其中列出了所有不包含stringB
.從
man grep
-v, --invert-match Invert the sense of matching, to select non-matching lines. (-v 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.)