我正在尝试搜索文件dep/playlist中的' john '。然后,我想将此结果应用于一个新的grep命令,该命令搜索文件员工列表,然后在屏幕上显示结果。下面的代码没有按预期运行。
grep ohn dep/playlist > search
grep $(cat search) employeelist > newlist
cat newlist
谢谢,蒂姆
您需要告诉grep
从文件中获取模式,使用-f
选项:
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file
contains zero patterns, and therefore matches nothing. (-f is
specified by POSIX.)
所以命令看起来像:
grep -f search employeelist > newlist
使用进程替换可以避免对临时文件的需要。所以两个grep
命令可以写成一个:
grep -f <(grep ohn dep/playlist) employeelist > newlist
xargs:
grep ohn dep/playlist | xargs -I name grep name employeelist
这将在dep/playlist
中搜索' john ',然后在找到结果后,将该结果用于grep X employeelist
,其中X
是来自第一个grep的结果。