【问题标题】:Showing only the content of a file from a directory(given as parameter ) or his subdirectories which has .txt extension, using grep使用 grep 仅显示目录(作为参数给出)或其具有 .txt 扩展名的子目录中的文件内容
【发布时间】:2021-03-25 10:23:13
【问题描述】:

我设法使用 find:

find 'directory' -type f -name "*.txt" -print0 | xargs -0 cat

我不知道如何使用 grep 来做到这一点。尝试了上千次。

【问题讨论】:

  • 为什么需要grep
  • edit 提出您的问题,并解释您究竟想用grep 做什么。是否要对所有匹配的文件运行grep 并最终得到grep 的输出,即将cat 替换为grep?或者您想使用grep 作为附加标准来选择应该使用cat 发送到标准输出的文件?或者你想用grep而不是find过滤文件名?
  • 需要 grep 因为我听说我也可以使用 grep 来做到这一点,而以其他方式做到这一点对我来说是一个挑战。我不知道它应该包含在哪里,尝试了很多方法。不过找到了完美的答案。

标签: linux grep


【解决方案1】:
ls -bR | grep -E '^*.\.txt$' | xargs cat

使用 -R 递归列出文件并使用 -b 转义任何不可打印的字符/空格。对以“.txt”结尾的任何条目的列表进行 Grep,然后将结果通过管道传输到 xargs cat。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    相关资源
    最近更新 更多