$ perl -Mstrict -Mwarnings -le ' my $cell = < 1. Network-Time, Protocol: TCP, Source Port: 0-65535, Destination Port: 13-13 2. Network-Time-1, Protocol: UDP, Source Port: 0-65535, Destination Port: 13-13 3. Network-Time-2, Protocol: TCP, Source Port: 0-65535, Destination Port: 37-37 4. Network-Time-3, Protocol: UDP, Source Port: 0-65535, Destination Port: 37-37 5. Network-Time-4, Protocol: UDP, Source Port: 0-65535, Destination Port: 123-123 EOD my $re = qr{Protocol:\s+(\w+).*?Destination Port:\s+(\S+)}m; my @extract; push @extract, join(":", $1, $2) while $cell =~ /$re/g; print "@extract"; ' TCP:13-13 UDP:13-13 TCP:37-37 UDP:37-37 UDP:123-123