my $str = "aaa authentication login\x0d\n ssid Rich\x0d\n authentication open\x0d\n authentication shared \x0d\n"; my $authentication; while ($str =~/authentication (\S+)/){ $authentication = $authentiction. $1. " "; } # At the end of the while loop # $authentication should have the value # $authentication = open shared