【发布时间】:2019-05-21 06:43:35
【问题描述】:
我是编写 shell 脚本的新手,想在目录中搜索扩展名为“*RETURN.DAT”的文件中的特定字符串。我要查找的字符串位于单独的文件中。我应该使用什么 unix 命令?我混淆了使用 find 和 grep。我还需要复制我在另一个目录中找到的文件。提前致谢。
【问题讨论】:
我是编写 shell 脚本的新手,想在目录中搜索扩展名为“*RETURN.DAT”的文件中的特定字符串。我要查找的字符串位于单独的文件中。我应该使用什么 unix 命令?我混淆了使用 find 和 grep。我还需要复制我在另一个目录中找到的文件。提前致谢。
【问题讨论】:
如果文件在a目录中您不需要find(man find:在目录层次结构中搜索文件 >) 但您可以指向 grep 从该目录中搜索:
$ grep search_term path/to/a/dir/*.DAT
由于[您]要查找的字符串位于单独的文件中,man grep:
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. If this option is used
multiple times or is combined with the -e (--regexp) option,
search for all patterns given. The empty file contains zero
patterns, and therefore matches nothing.
所以:
$ grep -f separate_file path/to/a/dir/*.DAT`
应该做的工作。
【讨论】: