GNU shell 在嵌套 diff 中同时执行多项操作

发布于 2025-01-11 10:33:06 字数 475 浏览 0 评论 0原文

我试图在一行中做一些事情,这是我的尝试:

coolc -P p good.cool | xargs -I {} sh -c "diff <(sed 's/@[0-9]+/@/g' {}) <(sed 's/@[0-9]+/@/g' good.out)"
  1. 我有一个名为 good.out 的文件,我想在其上运行 sed:< code>sed 's/@[0-9]+/@/g' good.out
  2. 我想运行 coolc -P p good.cool 它将结果打印到
  3. 我想要​​的 标准输出在 sed 中使用 (2) 的输出's/@[0-9]+/@/g' {}
  4. 我想比较 (1) 和 (3)

是否可以在不创建新的情况下完成所有这些操作文件和所有内容都在一行中?

I am trying to do a few things in a single line and this is my attempt:

coolc -P p good.cool | xargs -I {} sh -c "diff <(sed 's/@[0-9]+/@/g' {}) <(sed 's/@[0-9]+/@/g' good.out)"
  1. I have a file called good.out and I want to run sed on it: sed 's/@[0-9]+/@/g' good.out
  2. I want to run coolc -P p good.cool which prints result to stdout
  3. I want to use the output from (2) in sed 's/@[0-9]+/@/g' {}
  4. I want to diff (1) and (3)

Is it possible doing all of these without creating a new files and all in a single line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风筝有风,海豚有海 2025-01-18 10:33:06

假设原始代码使用 xargs 是错误的,并且您确实只想运行 diff 一次:

diff <(coolc -P p good.cool | sed 's/@[0-9]+/@/g') \
     <(sed 's/@[0-9]+/@/g' good.out)

Assuming that the original code was wrong to use xargs, and you really want to run diff only once:

diff <(coolc -P p good.cool | sed 's/@[0-9]+/@/g') \
     <(sed 's/@[0-9]+/@/g' good.out)
滥情稳全场 2025-01-18 10:33:06

当然可以对此进行优化(在同一个 good.out 文件上一遍又一遍地运行 sed 效率不高),但是尽可能短的翻译代码为有效的东西(假设实际上有一个很好的理由使用xargs):

#!/usr/bin/env bash
while IFS= read -r filename; do
  diff <(sed 's/@[0-9]+/@/g' "$filename") \
       <(sed 's/@[0-9]+/@/g' good.out)
done < <(coolc -P p good.cool)
  • 需要是bash,而不是sh用于进程替换语法可用。
  • coolc -P p good.cool | 的目的而言xargs ... 是对写入 coolc -P p good.cool 的标准输出的每个项目运行一次 ... ,最好用BashFAQ #1 while read 循环。
  • BashFAQ #24我在管道中的循环中设置变量。为什么循环结束后它们就消失了?或者,为什么我不能通过管道读取数据?——这解释了为什么使用 <( ) 来提供 while read 循环而不是一根管子。

There are certainly optimizations that could be done to this (running sed on the same good.out file over and over is not very efficient), but the shortest possible translation of your code to something that works (written assuming that there was ever actually a good reason to use xargs):

#!/usr/bin/env bash
while IFS= read -r filename; do
  diff <(sed 's/@[0-9]+/@/g' "$filename") \
       <(sed 's/@[0-9]+/@/g' good.out)
done < <(coolc -P p good.cool)
  • bash, not sh, needs to be used for process substitution syntax to be available.
  • Insofar as the purpose of coolc -P p good.cool | xargs ... is to run ... once per item written into the stdout of coolc -P p good.cool, this is better replaced with a BashFAQ #1 while read loop.
  • BashFAQ #24: I set variables in a loop that's in a pipeline. Why do they disappear after the loop terminates? Or, why can't I pipe data to read? -- this explains why the use of <( ) to feed the while read loop instead of a pipe.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文