use mdfind to get list of matches with context? I want to search a folder of PDFs to find a particular string and get back a list of matches with a little of the surrounding context.

Using:

mdfind -onlyin ~/Dropbox/PDFs 'term' &gt;&gt; ~/Dropbox/searches/term.txt


I can get a list of files that contain a match, but I would like to see a little of the surrounding context so that I can tell a little bit more about the match. Ideally, with the filename prepended.

Something like:

~/Dropbox/PDFs/file1.pdf: here is the term you were searching
~/Dropbox/PDFs/file1.pdf: with term limits
~/Dropbox/PDFs/file2.pdf: the German term for a 
~/Dropbox/PDFs/file3.pdf: come up with a term for
etc...
mdfind will only ever return filenames, it doesn't display any content, nor does it possess the ability to do so in looking at its man page.

So instead of using mdutil, I'd suggest using a pdfgrep instead. This tool is available via brew:

$ brew install pdfgrep


Once installed it works like this:

$ pwd
/Users/user1/pdf

$ pdfgrep website -r
./docs/misc/Web-Shells-rev2.pdf:Say I run into the following website:
./docs/misc/Web-Shells-rev2.pdf:home directory of a website for shared host web servers:
./docs/misc/Web-Shells-rev2.pdf:Many websites with upload capabilities still rely on client side validation (javascript). These are
./docs/misc/Web-Shells-rev2.pdf:reverse shell I've hosted the file on one of my websites. Just remember to rename the extension
./docs/misc/Web-Shells-rev2.pdf:the output file, we could overwrite IIS log files or even deface websites. Elements the user can


pdfgrep has the normal compliment of switches to do recursive -r as well as ignore case -i, or even do regex searches.