我位於一個包含大量.txt
文件的資料夾中,我想找到包含stringA
但不包含的所有文件stringB
(它們不一定位於同一行)。有誰知道如何做到這一點?
答案1
只要您的檔案名稱不包含空格、製表符、換行符(假設未修改的$IFS
)或通配符且不以 開頭-
,並且如果您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.)
答案2
使用 GNU 工具:
grep -lZ stringA ./*.txt |
xargs -r0 grep -L stringB
-L
、-Z
、-r
、-0
有時是 GNU 擴展,但並不總是在其他一些實作中找到。
答案3
#run loop for each file in the directory
for i in `ls -l | tail -n+2 | awk '{print $NF}'` ; do
#check if file contains "string B"
#if true then filename is not printed
if [[ `egrep "string B" $i | wc -l` -eq 0 ]] ; then
#check if file contains "string A"
#if false then file name is not printed
if [[ `egrep "string A" $i | wc -l` -gt 0 ]] ; then
#file name is printed only if "string A" is present and "string B" is absent
echo $i
fi
fi
done
檢查伯恩哈德的答案後:
grep -Le "string B" $(grep -le "string A" `ls`)
如果檔案名稱包含空格:
grep -L stringB $(grep -l stringA `ls -l | tail -n+2 | awk '{print $NF}' | sed -e 's/\s/\\ /g'`