【问题标题】:How can I pipe the results of grep to a perl one liner?如何将 grep 的结果通过管道传输到 perl one liner?
【发布时间】:2017-06-04 12:15:30
【问题描述】:

我有一个 grep 命令来查找需要替换值的文件。然后我有一个 perl one 行,需要在每个文件上执行以替换该文件中找到的变量。

如何将我的 grep 命令的结果通过管道传输到 perl one liner?

grep -Irc "/env/file1/" /env/scripts/ | cut -d':' -f1 | sort | uniq

/env/scripts/config/MainDocument.pl
/env/scripts/config/MainDocument.pl2
/env/scripts/config/MainDocument.pl2.bak


perl -p -i.bak -e 's{/env/file1/}{/env/file2/}g' /env/scripts/config/MainDocument.pl

感谢您的帮助。

【问题讨论】:

  • 您能否就@millsofmn 提供的答案提供反馈?

标签: bash perl grep pipe


【解决方案1】:

使用$(...) bash 语法。

perl -p -i.bak -e 's{/env/file1/}{/env/file2/}g' $(grep -Irc "/env/file1/" /env/scripts/ | cut -d':' -f1 | sort | uniq)

【讨论】:

    【解决方案2】:

    我会忘记 perl one 班轮,而是使用 xargs 和 sed。

    grep -Irc "/env/file1/" /env/scripts/ | cut -d':' -f1 | sort | uniq  | xargs sed -ibak ':/env/file1/:/env/file2/:'
    

    【讨论】:

      猜你喜欢
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 2015-03-03
      • 1970-01-01
      相关资源
      最近更新 更多