![](/img/trans.png)
[英]Bash script - how to insert value of variable as new line, 2 line after matching a pattern in text file
[英]bash script to find pattern in text file and return entire line
我需要創建一個bash腳本,它循環遍歷目錄中的一堆.txt文件,然后在每個.txt中搜索一個字符串,並返回該字符串出現的整行。
我知道如何查看目錄中的所有.txt文件,
我只需要指向正確的方向來搜索文件本身,並根據該行中的匹配返回一行
在一個目錄內
grep "search string" *.txt
搜索或轉到子目錄
find /full/path/to/dir -name "*.txt" -exec grep "search string" {} ;\
一點點補充。 添加-n
以返回該行。
grep -rn "foo" *.txt
有關更多信息,請參閱grep --help
。
你可以使用一個循環:
for i in $(find|grep .txt); do grep "search" "$i";
如果您還想在每次匹配時打印文件名,請將-H
添加到grep
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.