Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Piping result of command as an argument

I want to get the result of rpm -qa | grep something and then run the result I got rpm -ql result-i-got, and all in one line.

I have found this to work:

rpm -ql $(rpm -qa | grep something)

I wonder, is there any better or even different way of piping this result to get the same result of what I wrote above? Thanks.

like image 263
Moshe Avatar asked May 24 '16 09:05

Moshe


2 Answers

xargs are made for that:

rpm -qa | grep something | xargs rpm -ql
like image 156
Andreas Louv Avatar answered Oct 05 '22 23:10

Andreas Louv


You can actually provide wildcard arguments to rpm -qa; this avoids the need to pipe to another command so instead of rpm -qa | grep something, you can use

rpm -qa '*something*'

Use single or double quotes to prevent the shell from expanding the asterisk as a shell globbing pattern.

Example:

$ rpm -qa '*vim*'
vim-enhanced-7.0.109-7.2.el5
vim-minimal-7.0.109-7.2.el5
vim-common-7.0.109-7.2.el5

The above command can be combined with the -l option to list the files in the relevant packages.

rpm -qal '*vim*'

One problem with this command is that if more than one package is returned, it’s not clear which files belong to to which package as the package name is not printed. To include the package name in the output, you could add the --queryformat option to have finer control over how information is printed. E.g., the following example prints the name of the package, underlined by a row of hyphen-minus symbols, followed by the list of files provided by that package:

rpm -qal --qf "\n%{NAME}\n--------\n" '*vim*'

The rpm man page has more information on customising the output of the command.


If you find yourself using this command often, you could turn it into an alias:

alias rpmlist='rpm -qal --qf "\n%{NAME}\n--------\n"'

and use it like so

rpmlist '*vim*' | less
like image 25
Anthony Geoghegan Avatar answered Oct 06 '22 01:10

Anthony Geoghegan