簡體   English   中英

bash:檢查目錄中的多個文件是否包含列表中的字符串

[英]bash: check if multiple files in a directory contain strings from a list

民間,

我有一個文本文件,其中包含多行,每行一個字符串:

str1
str2
str3

等等..

我想讀取此文件的每一行,然后在位於不同目錄的多個文件中搜索那些字符串。

我不太確定如何進行。

非常感謝您的幫助。

使用GNU Grep的--file選項

根據grep(1):

   -f FILE, --file=FILE
          Obtain  patterns  from  FILE,  one  per  line.   The  empty file
          contains zero patterns, and therefore matches nothing.   (-f  is
          specified by POSIX.)

-H-n標志將打印每個匹配項的文件名和行號。 因此,假設您將模式存儲在/ tmp / foo中並想在/ tmp / bar中搜索所有文件,則可以使用類似以下內容的方法:

# Find regular files with GNU find and grep them all using a pattern
# file.
find /etc -type f -exec grep -Hnf /tmp/foo {} +
for wrd in $(cut -d, -f1 < testfile.txt); do grep $wrd dir/files* ; done
awk 'NR==FNR{a[$0];next} { for (word in a) if ($0 ~ word) print FILENAME, $0 }' fileOfWords /wherever/dir/*
while read -r str
do
   echo "$str"
   grep "$str" /path/to/other/files
done < inputfile

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM