【问题标题】:Search for files containing specific string in text file in unix在 unix 的文本文件中搜索包含特定字符串的文件
【发布时间】:2019-05-21 06:43:35
【问题描述】:

我是编写 shell 脚本的新手,想在目录中搜索扩展名为“*RETURN.DAT”的文件中的特定字符串。我要查找的字符串位于单独的文件中。我应该使用什么 unix 命令?我混淆了使用 find 和 grep。我还需要复制我在另一个目录中找到的文件。提前致谢。

【问题讨论】:

    标签: shell ksh


    【解决方案1】:

    如果文件a目录中您不需要findman 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` 
    

    应该做的工作。

    【讨论】:

      猜你喜欢
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 2012-10-07
      相关资源
      最近更新 更多