Linux 使用 grep 匹配 md5 哈希
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4505641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using grep to match md5 hashes
提问by Meredith
How can I match md5 hashes with the grep command?
如何将 md5 哈希与 grep 命令匹配?
In php I used this regular expression pattern in the past:
在 php 中,我过去使用过这个正则表达式模式:
/^[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.
和其他变体,但我没有得到任何输出,而且我确定该文件包含 md5 哈希值。
采纳答案by martona
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
回答by Ignacio Vazquez-Abrams
Meh.
嗯。
#!/bin/sh
while IFS=: read filename filesize hash
do
if [ -z "$hash" ]
then
echo "$filename"
fi
done < hashes.lst
回答by Glenn McAllister
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:
以下应该可以向您显示哪些行具有 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.
-E 用于扩展正则表达式支持,-i 用于忽略模式和输入文件中的注意事项。
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.
-v 反转匹配,因此它向您显示没有 MD5 的行。
回答by derFunk
A little one-liner which works cross platform on Linux and OSX, only returning the MD5 hash value (replace YOURFILE
with your filename):
一个在 Linux 和 OSX 上跨平台工作的小单行,只返回 MD5 哈希值(替换YOURFILE
为您的文件名):
[ "$(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