use warnings; use strict; use IPC::System::Simple qw/capturex/; my $vm_run = 'zabbix'; my $id; for ( capturex('xl','list') ) { # loop over lines of command output chomp; # remove newline my @F = split; # split the line into fields on whitespace (like awk) if ( $F[0] eq $vm_run ) { # match on the first field $id = $F[1]; # store the second field #last; # would be the equivalent of fgrep -m1 } } print "ID: $id\n"; # prints "ID: 2" in this example