I have a set of PDFs, and I am running
strings * | grep message
This returns the strings containing that method, is there anyway I can get grep to tell me which file it is from?
With GNU strings
, use
strings --print-file-name -- *
to get the results prefixed by the file name (like grep
).
strings --print-file-name * | grep ': message'
(the file names output by strings
are terminated by :
). The main question is whether the OP wants just the file names (then using grep -l
only is better), or whether he also wants the matched strings (then grep
will either complain about binary garbage, or output binary garbage, and none of these is helpful).
strings --print-file-name * | grep ': .*message'
is better.
foo: message
for instance)
grep
has no idea where the strings came from when using this method, because it never sees the filenames. If you want the filenames, use -l
and pass the glob to grep
directly:
grep -l message ./*
grep
like GNU grep
that supports reading binary files.
--
? It would not be POSIX, and I seriously doubt any pre-POSIX implementation would support binary file.
--
. I just tested.
With GNU grep
, you can do:
grep -Hobae '[[:print:]]*message[[:print:]]*' -- *
That will tell you the filename and offset within the file of every printable string containing message
.