【问题标题】:How to find a specific comman string from multiple text files?如何从多个文本文件中查找特定的命令字符串?
【发布时间】:2017-09-15 06:55:22
【问题描述】:

我有 10 个不同的文本文件,其中包含 700 个图像路径。我希望检索首字母为“BarackObama”的常见字母,即应检索所有常见的 Barack Obama。

我尝试了以下一组命令,但没有得到想要的结果:

grep --include=\*.{txt} -rnw '/home/ashutosh/Downloads/New_IIIT-CFW/' -e "Barack"
grep -Fwf results1.txt results2.txt results3.txt results4.txt results5.txt results6.txt results7.txt results8.txt results9.txt results10.txt

请提出更好的选择!!

【问题讨论】:

  • 是否理解正确:您想要其中包含“BarackObama”的图像路径?另外,请发布示例数据和预期输出,这样我们就不必猜测您想要什么。
  • 不够清楚

标签: shell unix command-line grep


【解决方案1】:

您可以使用以下命令之一。输出将显示文件列表及其路径。

  • find /path -type f -exec grep -ilr 'search' {} \;

  • 查找 /path -type f | xargs grep -ilr '搜索'

【讨论】:

    【解决方案2】:

    你可以试试这样,它会在你文件所在的目录中搜索字符串。

    grep -rnw '/path/to/somewhere/' -e "pattern"
    

    除此之外,--exclude, --include, --exclude-dir or --include-dir 标志可用于高效搜索:

    这只会搜索那些扩展名为 .c 或 .h 的文件:

    grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
    

    或者另一种方法是使用 xargs 来加速。

    find /path/to/dir -type f | xargs grep -l "stringtosearch"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-28
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      • 1970-01-01
      • 2013-10-20
      • 1970-01-01
      • 2021-05-13
      相关资源
      最近更新 更多