![](/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.