Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have MD5 file which lists filename and MD5 sum in the following way:

MD5 (file14n0001.xml.gz) = 04eb2524d271077ec8f1a0021779b171
MD5 (file14n0002.xml.gz) = 07e2b29645e9211cce9daf1ae27e7b99
MD5 (file14n0003.xml.gz) = c31cca187a32743441868822b02d83aa

I need to parse out only filename and MD5 sum; for example, the desired output is:

file14n0001.xml.gz 04eb2524d271077ec8f1a0021779b171
file14n0002.xml.gz 07e2b29645e9211cce9daf1ae27e7b99
file14n0003.xml.gz c31cca187a32743441868822b02d83aa

My tr (augmented with sed) expression is:

tr -d 'MD5() ' | sed 's/=/ /g'

But tr part of the expression also delete all 5s in lines so the output is

file14n0001.xml.gz 04eb224d271077ec8f1a0021779b171
file14n0002.xml.gz 07e2b2964e9211cce9daf1ae27e7b99
file14n0003.xml.gz c31cca187a32743441868822b02d83aa

I kindly ask for a pointer how to proceed.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

Command:

sed -r 's/MD5 .(.*). = (.*)/\1 \2/' filename

Output:

file14n0001.xml.gz 04eb2524d271077ec8f1a0021779b171
file14n0002.xml.gz 07e2b29645e9211cce9daf1ae27e7b99
file14n0003.xml.gz c31cca187a32743441868822b02d83aa
share|improve this answer

Don't use tr, just use sed.

sed -r -e 's/^MD5 [(](.+)[)] = ([0-9a-f]+)$/\1 \2/g'
share|improve this answer
    
Don't know why it returns Unmatched ( or \(? –  Andrej Jul 17 at 16:45
    
My bad, I should have put the code in a code block: this site interpreted some of the characters and did not format it properly. Now fixed –  richard Jul 18 at 8:22

For small files, a pure-bash solution is an option

while IFS='() ' read -r _ file_name _ sum; 
do 
   printf '%s %s\n' "$file_name" "$sum"; 
done < file
file14n0001.xml.gz 04eb2524d271077ec8f1a0021779b171
file14n0002.xml.gz 07e2b29645e9211cce9daf1ae27e7b99
file14n0003.xml.gz c31cca187a32743441868822b02d83aa
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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