GNU shell 在嵌套 diff 中同时执行多项操作
我试图在一行中做一些事情,这是我的尝试:
coolc -P p good.cool | xargs -I {} sh -c "diff <(sed 's/@[0-9]+/@/g' {}) <(sed 's/@[0-9]+/@/g' good.out)"
- 我有一个名为
good.out
的文件,我想在其上运行sed
:< code>sed 's/@[0-9]+/@/g' good.out - 我想运行
coolc -P p good.cool
它将结果打印到 - 我想要的 标准输出在 sed 中使用 (2) 的输出's/@[0-9]+/@/g' {}
- 我想
比较
(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)"
- I have a file called
good.out
and I want to runsed
on it:sed 's/@[0-9]+/@/g' good.out
- I want to run
coolc -P p good.cool
which prints result to stdout - I want to use the output from (2) in
sed 's/@[0-9]+/@/g' {}
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设原始代码使用 xargs 是错误的,并且您确实只想运行
diff
一次:Assuming that the original code was wrong to use xargs, and you really want to run
diff
only once:当然可以对此进行优化(在同一个
good.out
文件上一遍又一遍地运行sed
效率不高),但是尽可能短的翻译代码为有效的东西(假设实际上有一个很好的理由使用xargs):bash
,而不是sh
用于进程替换语法可用。coolc -P p good.cool | 的目的而言xargs ...
是对写入coolc -P p good.cool
的标准输出的每个项目运行一次...
,最好用BashFAQ #1while read
循环。<( )
来提供while read
循环而不是一根管子。There are certainly optimizations that could be done to this (running
sed
on the samegood.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):bash
, notsh
, needs to be used for process substitution syntax to be available.coolc -P p good.cool | xargs ...
is to run...
once per item written into the stdout ofcoolc -P p good.cool
, this is better replaced with a BashFAQ #1while read
loop.<( )
to feed thewhile read
loop instead of a pipe.