LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   how to set sed to show random md5 hash! (https://www.linuxquestions.org/questions/linux-general-1/how-to-set-sed-to-show-random-md5-hash-642353/)

Dr_Death_UAE 05-15-2008 06:47 AM

how to set sed to show random md5 hash!
 
Hello, does anyone know how to make sed get the md5 checksum hash from a file.

I have some files that have random md5 checksum hash on it content, trying to figure out how i can set sed to know the md5 hash between all the lines and words in the files.

md5 is a 32 character string of a random hexadecimal numbers [0-9][a-f].

i tried to set identifier in sed to show the hash after it, but it also show the worlds in the same line after the hash.

any idea ?

jschiwal 05-15-2008 06:54 AM

You will have to show what you have so far, or a sample line before someone can provide much help.

You probably want to use the form:
sed '/<md5pattern>/s/.*\(<md5pattern>\).*/\1/' file
to select the line with the md5sum and to only output the match.

Dr_Death_UAE 05-15-2008 07:09 AM

no jschiwal, i dont have some thing to match.

some of the files they have "md5sum=" before the hash, so i can get the hash, but it show also words after the hash for example:

file:
Quote:

filename=test, md5sum=764efa883dda1e11db47671c4a3bbd9e, size=402KB
sed command:
Quote:

sed 's/.*md5sum=[0-9][a-f]*//' file
will show:
Quote:

md5sum=764efa883dda1e11db47671c4a3bbd9e, size=402KB
i was wounder how to set sed to know the hash base on its length 32 char, and it is have [0-9][a-f]

thanks

colucix 05-15-2008 07:18 AM

Why not gawk?
Code:

gawk '/md5sum=/{print substr($0,index($0,"md5sum=")+7,32)}' file
or if you want to print the "md5sum=" part, too
Code:

gawk '/md5sum=/{print substr($0,index($0,"md5sum="),39)}' file

Dr_Death_UAE 05-15-2008 07:31 AM

thanks colucix, awk work fine, is there a way to get the hash without any identifier, i mean if the "md5sum=" is a variable and the hash also a variable. not all of the files have "md5sum=".

thanks again

colucix 05-15-2008 08:36 AM

So you want to match any sequence of 32 characters matching digits or letter from a to f (hexadecimal digits), right? Something like this should work
Code:

gawk --posix '{idx = match($0,/[0-9a-f]{32}/); if ( RLENGTH == 32 ) print substr($0,idx,RLENGTH)}' file
Please note that you have to specify the --posix option of awk to let it recognize the braced interval expression {32} in the regular expression, which is disabled by default.

Dr_Death_UAE 05-15-2008 09:44 AM

Thanks colucix, it didn't work. but i got the idea. thanks a gain.


Quote:

h4x0r# echo "test" | md5 > test
h4x0r# cat test
d8e8fca2dc0f896fd7cb4cb0031ba249
h4x0r# awk --posix '{idx = match($0,/[0-9a-f]{32}/); if ( RLENGTH == 32 ) print substr($0,idx,RLENGTH)}' test
h4x0r#

colucix 05-15-2008 11:16 AM

Strange... it works on my system. Which version of awk do you have?

Dr_Death_UAE 05-15-2008 02:39 PM

h4x0r# awk --version
awk version 20070501 (FreeBSD)

colucix 05-15-2008 03:13 PM

Hmmm... once I read about the FreeBSD version of awk not supporting extended regular expressions and in particular the interval expression I used in the command above. I don't know much about FreeBSD, but as alternative you can try something like
Code:

grep -E -o '[0-9a-f]{32}' file
I'm not sure if grep on FreeBSD supports the -o option, to print the matching part only.

Dr_Death_UAE 05-15-2008 03:22 PM

grep work fine thanks dude, is there a difference between awk and gawk?

colucix 05-15-2008 03:34 PM

Yes. A lot of differences, in the sense that the GNU awk (gawk) has introduced new advanced features not available in standard unix's awk. Portability of your awk code is an issue when you are used to the GNU extension of awk.

Dr_Death_UAE 05-15-2008 03:49 PM

aha, thanks for the info brother.

Dr_Death_UAE 05-16-2008 06:48 AM

i install gawk on my FreeBSD, the posix work fine with it.

Quote:

h4x0r# gawk --version
GNU Awk 3.0.6 + multi-byte extension 1.15
h4x0r# cat md5test
filename=test, md5sum=764efa883dda1e11db47671c4a3bbd9e, size=402KB
h4x0r#
h4x0r# gawk --posix '{idx = match($0,/[0-9a-f]{32}/); if ( RLENGTH == 32 ) print substr($0,idx,RLENGTH)}' md5test
764efa883dda1e11db47671c4a3bbd9e
h4x0r#
thanks

colucix 05-16-2008 07:55 AM

Good to know! You're welcome! :)


All times are GMT -5. The time now is 05:44 PM.