my @lines = `list cm device all-properties`; my @matches = grep { /configsync-ip/ } @lines; chomp @matches; # Remove trailing newlines #### my @matches = grep { /configsync-ip/ } `list cm device all-properties`; chomp @matches; #### for (@matches) { my (undef, $ip) = split /\s+/; ... # Your code to deal with the IP here. } #### my @ips = map { (split /\s+/)[1] } grep { /configsync-ip/ } `list cm device all-properties`; #### use List::MoreUtils qw/:all/; my $it = natatime 2, `tmsh list net self`; while (my ($addr, $allow_service) = $it->()) { # $addr is the first line, e.g.: address 1.1.1.1/24 # $allow_service is the 2nd line: allow-service all }