简体   繁体   English

使用命令的输出作为 grep 参数

[英]use output of command as grep argument

I'm trying to use output of another command as grep argument (I think this feature is known as Bash process substitution ).我正在尝试使用另一个命令的输出作为 grep 参数(我认为此功能称为Bash 进程替换)。 But the problem is that it does not work, grep did not find any entries但问题是它不起作用,grep没有找到任何条目

wakatana@ubuntu:~$ dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | grep  <(uname -mrs | awk '{print $2}')
wakatana@ubuntu:~$

But when I first assign it to variable then it works:但是当我第一次将它分配给变量时,它就起作用了:

wakatana@ubuntu:~$ pattern=$(uname -mrs | awk '{print $2}')
wakatana@ubuntu:~$ dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | grep  $pattern
8222    linux-image-4.15.0-118-generic
12894   linux-headers-4.15.0-118-generic
64165   linux-modules-4.15.0-118-generic
165817  linux-modules-extra-4.15.0-118-generic

Output of desired command is following所需命令的输出如下

wakatana@ubuntu:~$ uname -mrs | awk '{print $2}'
4.15.0-118-generic

But when I echo result of process substitiution I get this:但是当我回显过程替换的结果时,我得到了这个:

wakatana@ubuntu:~$ echo  <(uname -mrs | awk '{print $2}')
/dev/fd/63

What I'm doing wrong?我做错了什么?

OS info:操作系统信息:

wakatana@ubuntu:~$ bash --version
GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

wakatana@ubuntu:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.5 LTS
Release:        18.04
Codename:       bionic

wakatana@ubuntu:~$ uname -a
Linux ubuntu 4.15.0-118-generic #119-Ubuntu SMP Tue Sep 8 12:30:01 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

You do not want to read from the stream associated with the output of command, you want to pass the whole result of a command as argument to another command.您不想从与命令输出关联的流中读取,而是希望将命令的整个结果作为参数传递给另一个命令。 So you want:所以你要:

dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | grep "$(uname -mrs | awk '{print $2}')"

What I'm doing wrong?我做错了什么?

Well, I guess you wrongly expect process substitution to be substituted for the content of the output of a command, while it is being substituted with a single filename associated with the output of the command.好吧,我猜你错误地期望进程替换被替换为命令输出的内容,而它被替换为与命令输出关联的单个文件名 To replace the command by the output produced by that command use command substitution .要将命令替换为该命令产生的输出,请使用命令替换 For further research I advise bash manual process substitution , bash manual command substitution .为了进一步研究,我建议使用bash 手动进程替换bash 手动命令替换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM