开发者

How can I pass the multi-line output of a function to a grep?

开发者 https://www.devze.com 2023-04-08 15:07 出处:网络
I\'m new to UNIX and having trouble with what is probably a very simple concept. I would like to take the output of an awk designed to return a single column of data (in this case, it\'s a couple of l

I'm new to UNIX and having trouble with what is probably a very simple concept. I would like to take the output of an awk designed to return a single column of data (in this case, it's a couple of lines containing FC WWNs) and use that as a search parameter in a grep the same way I would use a file containing the column.

My first thought was to simply type grep -f awk {'print $3'} myfile myotherfile

Clearly, that didn't work. I know I could do this simply with two commands and an intermediate file (outputting my awk with a > to a new file, and the using the grep -f with it), but I would like to know 开发者_开发知识库if there's a way to do this without adding files.

Background: I'm using AIX 5.3 and bash


You can use the nifty $< bash idiom of bash to replace a file name argument with the output of a specified command.

fgrep -f <(awk '{print $3}' myfile) myotherfile


A common idiom that appears to work with GNU grep (don't know about AIX grep) is to use - as a specification for stdin. So in your case, if I've interpreted the commanded correctly, you'd use:

awk {'print $3'} myfile | grep -f - myotherfile

0

精彩评论

暂无评论...
验证码 换一张
取 消