#! perl -sw use strict; my $exp= qr/^DMSC0022\s+\S+\s+(\w+)$/; my $str='Logfile_Connection_Lost Hostname: ?'; $_="DMSC0022 X10 oswald"; if ( m/$exp/) { my $bit = $1; $str =~ s/\?/$bit/; print $str; }