0

and apologies for the newb question. I'm writing a perl script to check an MD5SUM against itself before running. Think of it as a "do not change the script check". I've managed to get the FCIV binary to calculate the MD5SUM, but there is no way to obtain just the MD5SUM as a single string output.

It spews the following to the console:

    # //
    # // File Checksum Integrity Verifier version 2.05.
    # //
    # 3e593beb3bb51a23f5a2ccae0f2c70a3 perlscript.pl

I want to procress this multi line string, capturing just the md5sum. I'm presuming the best way to do this would be with a substitution regular expression. Unfortunately though but I'm having problems with the string being over multiple lines, it emptys the whole thing.

1) Is there a better way to process this output other than a regex? 2) If we are using a regex, what would you suggest?

This is what I'm trying to us:

    $md5val =~s /(\/\/[.])*\n//;

I'm pretty new to all this, to any comments are appreciated. Thanks in advance.

0

3 Answers 3

1

One possible way: split on newlines, take the last line, take its second word:

#!/usr/bin/perl
use warnings;
use strict;

my $md5val = '    # //
    # // File Checksum Integrity Verifier version 2.05.
    # //
    # 3e593beb3bb51a23f5a2ccae0f2c70a3 perlscript.pl
';

$md5val = (split ' ', (split /\n/, $md5val)[-1])[1];
print "<$md5val>\n"
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, perfect, thanks! That was a very quick reply. I've amended it slightly because the string didn't use # at the start of each line, otherwise it works as decribed.
1

The simple and straightforward solution is to use Perl's standard library. In particular, Digest::MD5 already does what you want.

use Digest::MD5 qw(md5_hex);
my $md5val = md5_hex($data);

1 Comment

Thanks for the reply. I'll bear this in mind for future development. I didn't use this library because I need to be able to generate the MD5SUM and hard code it into a config file that's subsequently read by the script. This means bundling fciv. However, once the script is complete I could subsitute to this.
1

Instead of using substitution to remove everything you don't want, use a capture group to extract the part you do:

use warnings;
use strict;

my $md5val = do {local $/; <DATA>};

my ($md5sum) = $md5val =~ /\b([0-9a-f]{32})\b/;

print "$md5sum\n";

__DATA__
    # //
    # // File Checksum Integrity Verifier version 2.05.
    # //
    # 3e593beb3bb51a23f5a2ccae0f2c70a3 perlscript.pl

Outputs:

3e593beb3bb51a23f5a2ccae0f2c70a3

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.