in reply to Re: search text file
in thread search text file

To count all the matches from the command line:

grep -oF -f firstfile secondfile | sort | uniq -c

Replies are listed 'Best First'.
Re^3: search text file
by ambrus (Abbot) on Jul 27, 2011 at 11:51 UTC

    Ah, good idea using grep -o. That does indeed find multiple matches in a single line.

    That, however, won't work correctly if some of the matches are overlapping. Eg. if the second file has abcdef and the first file has the two strings abcd and cdef, grep will only find the abde part. As a workaround, you could run grep once for each string in the first file. Thus, we get (I think)

    ( while read; do grep -oFe "$REPLY" secondfile | wc -l; done ) < first +file