#!/usr/bin/perl use warnings; use strict; my $msg = ""; my $log_file = "Output.txt"; open my $fh, '<', $log_file or die "can't open file: $!"; while ( my $line = <$fh> ) { chomp $line; if ( $line =~ /^\bstart.+?/i ) { $msg .= $line. ' '; } elsif ( $line =~ /^\bresponse.+?$/i ) { $msg .= $line. $/; } else { next; } } close $fh or die "can't close file:$!"; print $msg;