在 shell (bash) 中,如何在管道中执行多个命令?
我正在尝试在 Bash shell 中一次通过多个命令通过管道传输 awk 命令的输出,根据我的知识,我想到了这一点:
awk '$13 ~ /type/ {print $15}' filename.txt | (wc -l || sort -u)
我希望对 awk 命令的结果进行计数和排序,如何我能做到吗? 即使有 &&命令它不起作用,它执行第一个命令然后退出。 我想这是我对 bash 的了解失败了。
提前致谢。
I'm trying pipe the output of an awk command through more than one command at once in a Bash shell, following my knowledge I'm coming up with this:
awk '$13 ~ /type/ {print $15}' filename.txt | (wc -l || sort -u)
I want the result of the awk command to be both counted AND sorted, how can I accomplish that?
Even with && command it doesn't work, it does execute the first command and then exits.
I guess it is my knowledge of bash which is failing.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想在一行中将输出发送到两个不同的命令,则需要进行过程替换。
试试这个:
这会在 stderr 上输出行数,并在 stdout 上输出排序后的输出。如果您需要 stdout 上的行数,则可以省略
>&2
,但随后它将传递给排序调用并(很可能)排序到顶部输出。编辑:根据进一步测试更正了所发生情况的描述。
If you want to send output to two different commands in a single line, you'll need to do process substituion.
Try this:
This outputs the line count on stderr and the sorted output on stdout. If you need the line count on stdout, you can do that leave off the
>&2
, but then it will be passed to the sort call and (most likely) sorted to the top of the output.EDIT: corrected description of what happens based on further testing.
在这种情况下,你用 awk 来计数,为什么需要管道?不要让事情变得更复杂
in that case, do your counting in awk , why the need for pipes? don't make it more complicated
如果输出的大小不太适合内存,并且出于性能原因您不需要
wc
和sort
命令并行工作,那么这里有一个相对简单的解决方案:额外的
a
的复杂性是为了防止awk
命令可能在输入末尾打印一些空行,其中$()
构造将被剥离。您可以轻松选择sort
或wc
应首先出现。这是一种适用于任何 POSIX shell(ash、bash、ksh、zsh...)的方法,但仅适用于具有
/dev/fd
的系统(其中包括相当新的 Linux、*BSD 和 Solaris) )。就像Walter 的类似构造使用了 bash、ksh93 和 zsh 中可用的更简单方法,wc
的输出和sort
的输出可能会混合。如果您都需要处理不太适合内存的中间输出,并且不想将两个命令的输出混合在一起,我认为没有一种简单的方法POSIX shell,尽管它应该可以与 ksh 或 zsh 协进程一起使用。
If the size in the output is not too large to fit in memory and you don't need the
wc
andsort
commands to work in parallel for performance reasons, here's a relatively simple solution:That complication with the the extra
a
is in case theawk
command might print some empty lines at the end of the input, which the$()
construct would strip. You can easily choose which ofsort
orwc
should appear first.Here's a way that works with any POSIX shell (ash, bash, ksh, zsh, ...) but only on systems that have
/dev/fd
(which includes reasonably recent Linux, *BSD and Solaris). Like Walter's similar construction using the simpler method available in bash, ksh93 and zsh, the output ofwc
and the output ofsort
may be intermixed.If you both need to deal with intermediate output that doesn't comfortably fit in memory and don't want to have the output of the two commands intermixed, I don't think there's an easy way in a POSIX shell, though it should be doable with ksh or zsh coprocesses.
您希望将 mkfifo 创建的命名管道与 tee 结合使用。示例位于 http://www.softpanorama.org/Tools/tee.shtml
You want to use named pipes created with mkfifo in combination with tee. An example is at http://www.softpanorama.org/Tools/tee.shtml
我认为更大的问题是:您期望输出是什么?
如果你想做两件事,那就做两件事:
I think the bigger question is: what are you expecting the output to be?
If you're trying to do two things, then do two things: