pseudo code check line for tabMSISDN=digits; if found extract digits and assign to line otherwise assign MSISDN=notSet to the line for printing later #### if (/^(\t*MSISDN=(\d+));/) { print OUTFILE "Update Command $line\n" if defined $line; $line = "<$2>"; #group 2 #otherwise assign MSISDN=notSet to the line for printing later } #### #!/usr/bin/perl use strict; use warnings; my $HSSIN='D:\testproject\sample-input.txt'; my $ofile = 'D:\testproject\sample-output.txt'; my $add; open (INFILE, $HSSIN) or die "Cant open input file"; open (OUTFILE,"> $ofile" ) or die "Cant open file"; my $line; while () { if (/^(\t*MSISDN=(\d+));/) { print OUTFILE "Update Command $line\n" if defined $line; $line = "<$2>"; #group 2 } if (/(\t*ODBIC=([\w]+?\w.*));/) { #print OUTFILE "$line\n" if defined $line; #$line = $2; $add = $2; $line .= ",$add"; } if (/(\t*ODBOC=([\w]+?\w.*));/) { $add = $2; $line .= ",$add"; } } print OUTFILE "Update Command $line\n"; close INFILE; close OUTFILE; #### ## desired output Update Command <123476789678>,BIC,BAOC Update Command MSISDN=notSet,BIC, BAOC