0

I have a file where I want to grep for an md5 hash. I was able to do that but how can I display the match to stdout?

When I do grep -e "[0-9a-f]\{32\}" file

I just get:

Binary file file matches. Is there a way to print the result to stdout?

3
  • 2
    is it a binary file or is grep just getting confused by, e.g., unicode characters in the filenames inside file? if it's not really a binary file, try grep -a .... If it is binary, try grep -a -o ... or something like strings file | grep ... Commented Sep 25, 2019 at 13:35
  • 1
    You're expecting file to be a text file? Commented Sep 25, 2019 at 13:38
  • 1
    Seems like your file (which we don't know) has some stuff that confuses GNU grep. But sed -nE 's/.*([0-9a-f]{32}).*/\1/p' file should do the job Commented Sep 25, 2019 at 13:51

1 Answer 1

2

From https://www.gnu.org/software/grep/manual/html_node/Usage.html

Why does grep report “Binary file matches”?

If grep listed all matching “lines” from a binary file, it would probably generate output > that is not useful, and it might even muck up your display. So GNU grep suppresses output > from files that appear to be binary files. To force GNU grep to output lines even from files that appear to be binary, use the -a or ‘--binary-files=text’ option. To eliminate > the “Binary file matches” messages, use the -I or ‘--binary-files=without-match’ option.

2
  • This does output the hash correctly, however it also outputs some more junk afterwards that is not part of the pattern.. Is there a way to prevent this? The command used was: grep -e "[0-9a-f]\{32\}" file --binary-files=text Commented Sep 25, 2019 at 13:46
  • 1
    You can use grep -o to output only the match, instead of the whole line: grep -e -o --binary-files=text"[0-9a-f]\{32\}" file Commented Sep 25, 2019 at 14:08

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.