use strict; use warnings; my $input_file = $ARGV[0]; #input file name my $input_pid = $ARGV[1]; #input file name my $input_string = $ARGV[2]; #input string to be searched in the matched string print "Usage: test.pl " if (@ARGV != 3); open (my $hfile, '<', "$input_file") || die("Unable to open the file $input_file\n"); open (my $ofile, '>', "output.txt") || die("Unable to open the file output.txt\n"); while (<$hfile>){ #read the file line by line my $line = $_; if ($line =~ /[DWEM]\s+.*?\s+(?:c|cm|M)\:($input_pid)\s+([^\.]+\.c)/s) { my $c_file = $2; my $pid = $1; if($input_string eq $c_file){ #check with input string and if matches print $ofile "$line\n"; #write the output in a output file } } } Command Line: ------------- >perl test.pl a.txt 10570 'cm_child.c' File output.txt: ---------------- PID:10570 C File:cm_child