12

How can I match md5 hashes with the grep command?

In php I used this regular expression pattern in the past:

/^[0-9a-f]{32}$/i

But I tried:

grep '/^[0-9a-f]{32}$/i' filename
grep '[0-9a-f]{32}$/' filename
grep '[0-9a-f]{32}' filename

And other variants, but I am not getting anything as output, and i know for sure the file contains md5 hashes.

4 Answers 4

15

You want this:

grep -e "[0-9a-f]\{32\}" filename

Or more like, based on your file format description, this:

grep -e ":[0-9a-f]\{32\}" filename
3

Well, given the format of your file, the first variant won't work because you are trying to match the beginning of the line.

Given the following file contents:

a1:52:d048015ed740ae1d9e6998021e2f8c97
b2:667:1012245bb91c01fa42a24a84cf0fb8f8
c3:42:
d4:999:85478c902b2da783517ac560db4d4622

The following should work to show you which lines have the md5:

grep -E -i '[0-9a-f]{32}$' input.txt

a1:52:d048015ed740ae1d9e6998021e2f8c97
b2:667:1012245bb91c01fa42a24a84cf0fb8f8
d4:999:85478c902b2da783517ac560db4d4622

-E for extended regular expression support, and -i for ignore care in the pattern and the input file.

If you want to find the lines that don't match, try

grep -E -i -v '[0-9a-f]{32}$' input.txt

The -v inverts the match, so it shows you the lines that don't have an MD5.

2
  • Err... didn't the original question have a description of the file he was grepping through? The answers we've provided don't entirely make sense in the new, less detailed, question. Dec 22, 2010 at 5:24
  • +1 for taking the effort to explain what was causing the failed match by grep.
    – GuruM
    Jul 30, 2012 at 12:10
0

Meh.

#!/bin/sh
while IFS=: read filename filesize hash
do
  if [ -z "$hash" ]
  then
    echo "$filename"
  fi
done < hashes.lst
0

A little one-liner which works cross platform on Linux and OSX, only returning the MD5 hash value (replace YOURFILE with your filename):

[ "$(uname)" = "Darwin" ] && { MD5CMD=md5; } || { MD5CMD=md5sum; } \
    && { ${MD5CMD} YOURFILE | grep -o "[a-fA-F0-9]\{32\}"; }

Example:

$ touch YOURFILE
$ [ "$(uname)" = "Darwin" ] && { MD5CMD=md5; } || { MD5CMD=md5sum; } && { ${MD5CMD} YOURFILE | grep -o "[a-fA-F0-9]\{32\}"; }
d41d8cd98f00b204e9800998ecf8427e

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.