【问题标题】:Shell script to search a string and output lines for specific files in all directories用于在所有目录中搜索特定文件的字符串和输出行的 Shell 脚本
【发布时间】:2017-10-13 11:26:25
【问题描述】:

如何在所有目录的特定文件中搜索字符串并将结果输出到文件中?

我正在使用下面的代码,但我怎样才能为此编写一个 shell 脚本?

find. -name *.txt | grep *%text* > result.xls 

【问题讨论】:

  • 您可能更喜欢find . -type f -name "*.txt" -execdir grep '*%text*' '{}' + > results.txt

标签: string shell file search


【解决方案1】:

我不明白你的意思。我认为您的命令行可以正常工作。 要将其制作成脚本,只需将此命令行保存到文本文件中,并添加 x 权限。

  1. 将以下内容保存到文件中。例如:xxx.sh

    #!/bin/bash
    find . -name '*.txt' | grep '*%text*' > result.xls
    
  2. 通过以下命令为文件添加 x 权限。

    chmod +x xxx.sh
    
  3. 运行脚本。

    ./xxx.sh
    
  4. 检查此输出文件result.xls

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-05
    • 2020-01-03
    • 2011-01-20
    • 2016-07-12
    • 1970-01-01
    • 2017-09-18
    • 2014-03-20
    • 2012-03-11
    相关资源
    最近更新 更多