I have no good regex experience -- its all pretty new and confusing to me. I understand some of the VERY basics, but to do what im looking for...is way past me! I have the following text i would like to parse, pulling out the highlighted section everytime.

Unknown Trap from 192.168.200.35 : Enterprise [1.3.6.1.4.1.6876.4.1] : Specific [3] : Generic [6] : Varbinds [1.3.$
[1.3.6.1.4.1.6876.50.102 =>
/vmfs/volumes/4c8157c9-4cbe22b0-9192-0019b9e2f54d/vm-jetmore/vm-jetmore.vmx]
[1.3.6.1.4.1.6876.2.1.1.2.1 => vm-jetmore]


What i would like to be able to get for each instance this occurs, is just the last part... the vm name (bold section):

[1.3.6.1.4.1.6876.2.1.1.2.1 => vm-jetmore]

But i would want to pull from this area of the code each time, it will always be right after this value 1.3.6.1.4.1.6876.2.1.1.2.1 => ...

Is this possible? Could i have some help in doing this? Thank you so much in advance!

Recommended Answers

All 3 Replies

A couple of quick questions...
1. Is that one line of the file you are parsing? That is, will the => vm-jetmore] be at the end of a single line or does it span multiple lines?
2. Are those numbers always the same? Or do they change?

As for whether it is possible, the answer to that is "yes, very possible".

Assuming the example, which I hardcoded and assigned to $data, spans multiple lines and the value preceding the vm name will always be the same, you could try this.

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

my $data = <<END;
Unknown Trap from 192.168.200.35 : Enterprise [1.3.6.1.4.1.6876.4.1] : Specific [3] : Generic [6] : Varbinds [1.3.$
[1.3.6.1.4.1.6876.50.102 =>
/vmfs/volumes/4c8157c9-4cbe22b0-9192-0019b9e2f54d/vm-jetmore/vm-jetmore.vmx]
[1.3.6.1.4.1.6876.2.1.1.2.1 => vm-jetmore]
END

$data =~ /\Q[1.3.6.1.4.1.6876.2.1.1.2.1 =>\E\s*(.+?)\]/;
my $vm = $1;
print "The vm name is $vm\n";

I should have waited until you answered mitchems questions before jumping in. To clarify the above solution: if the string you test contains only one vm-name then the regex $data =~ /\Q[1.3.6.1.4.1.6876.2.1.1.2.1 =>\E\s*(.+?)\]/; should work for you. If your data file contains more than one vm-name, but no more than one per line you can still retrieve them all by reading and testing one line at a time.

However, if your data contains more than one vm-name and you read all the data into one scalar variable then my regex will find only the first vm-name. In that case, you can add the g flag to the regex and use it in list context, as follows:

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

my $data = <<END;
Unknown Trap from 192.168.200.35 : Enterprise [1.3.6.1.4.1.6876.4.1] : Specific [3] : Generic [6] : Varbinds [1.3.$
[1.3.6.1.4.1.6876.50.102 =>
/vmfs/volumes/4c8157c9-4cbe22b0-9192-0019b9e2f54d/vm-jetmore/vm-jetmore.vmx]
[1.3.6.1.4.1.6876.2.1.1.2.1 => vm-jetmore]
more data
[1.3.6.1.4.1.6876.2.1.1.2.1 => vm-newwave]
Another one
[1.3.6.1.4.1.6876.2.1.1.2.1 => vm-failsafe]
END

my @vms = $data =~ /\Q[1.3.6.1.4.1.6876.2.1.1.2.1 =>\E\s*(.+?)\]/g;

print "Found the following vm-names:\n", join("\n", @vms);

Outputs the following:

Found the following vm-names:
vm-jetmore
vm-newwave
vm-failsafe
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.