Possible to return number of matches per line of a regex file using `grep -f regexfile queryfile`?

时间:2015-10-30 22:34:37

标签: unix grep

I'm wondering if there is a simple way to transform a grep command such as

grep -c -f regex.txt file.txt

to return the total number of matched lines in file.txt for each line of regex.txt, instead of the sum of the matched lines found for all patterns in regex.txt as the above command does.

My current method of handling this is to use xargs (or GNU parallel interchangeably):

cat regex.txt | xargs -I{} grep -c {} file.txt

Can grep do this in one fell swoop?

1 个答案:

答案 0 :(得分:1)

grep -o -f regex.txt | sort | uniq -c