【问题标题】:grep output to TAR [duplicate]grep 输出到 TAR [重复]
【发布时间】:2017-03-07 19:17:17
【问题描述】:

我知道我在这里做了一些愚蠢的事情,但无法让它发挥作用。

我需要 grep 一个文件并从该 grep 输出中创建一个 TAR 文件

grep -a -E '*abcdefg*|*123*|hi' test.txt | tar -czvf test.rar.gz -T -

命令失败并出现以下错误

  tar\: abcdefg\t\1488877199\Link\t\thttps\\\://abcdefg//724974226928ars\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t1Cannot 

stat\: No such file or directory: Cannot stat: No such file or directory
    tar: Exiting with failure status due to previous errors

【问题讨论】:

  • test.txt 可能不存在。尝试使用有效的文件名。
  • @afr0ck test.txt 确实存在,我可以在错误消息中看到 grep 输出(现已在上面更新)
  • 将第一个命令的输出重定向到文件(grep .... > 输出),然后在生成的输出文件上启动 tar 命令(不要使用管道,而是使用分号;分开两个命令)。

标签: bash shell pipe


【解决方案1】:

-T 参数指定要放入存档的文件列表。这就是您要在 STDIN 上提供的内容,而不是实际数据。

-T 文件名

在 x 或 t 模式下,tar 将读取要从文件名中提取的名称列表。在 c 模式下,tar 将从文件名中读取要归档的名称。单独一行上的特殊名称“-C”将导致当前目录更改为下一行指定的目录。除非指定了 --null ,否则名称由换行符终止。请注意,--null 也会禁用对包含“-C”的行的特殊处理。

无论如何,you can't archive from STDIN。您需要先将数据放入中间文件。

grep -a -E '*abcdefg*|*123*|hi' test.txt > tempfile
tar -czvf test.tar.gz tempfile

或者,只使用 gzip 单独:

grep -a -E '*abcdefg*|*123*|hi' test.txt | gzip > test.gz

【讨论】:

  • 感谢您的解释,现在我知道问题所在了。
【解决方案2】:

你可以试试这样的 -

grep -a -E '*abcdefg*|*123*|hi' test.txt > ff &&  tar -czvf test.rar.gz ff

【讨论】:

    猜你喜欢
    • 2012-12-09
    • 2020-01-21
    • 2019-02-18
    • 1970-01-01
    • 2016-12-10
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    相关资源
    最近更新 更多