Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

searching for string & number..

by jaggu_bg (Initiate)
on Nov 27, 2008 at 08:27 UTC ( #726337=perlquestion: print w/replies, xml ) Need Help??

jaggu_bg has asked for the wisdom of the Perl Monks concerning the following question:

hi Monks... i have to search for the PID ( number) and a string in a file . after searching in log file it should print in a output file. the input is like.. perl test.pl -f logfile -o new -p 10570 -s cm_child.c and output should have all the lines from one D/E/M/W to other.... as all lines start with anyone of this.
D Fri Nov 21 14:09:41 2008 TME_BILLING_COE cm:10570 cm_child.c(107) +:4385 1:blrdxp-santbs:CustomerCenter:0:AWT-EventQueue-0:83:1227256900 +:0 XXX CMAP: op_custom() past op_decode, opcode: PCM_OP_CUST_VALI +DATE_CUSTOMER D Fri Nov 21 14:09:41 2008 TME_BILLING_COE cm:10570 fm_cust_validat +e_customer.c:92 1:blrdxp-santbs:CustomerCenter:0:AWT-EventQueue-0:83: +1227256900:0 op_cust_validate_customer input flist # number of field entries allocated 20, used 3 0 PIN_FLD_POID POID [0] 0.0.0.1 /plan -1 0 0 PIN_FLD_ACCOUNT_OBJ POID [0] 0.0.0.1 /account -1 0 D Fri Nov 21 14:09:41 2008 TME_BILLING_COE cm:10570 fm_cust_pol_pre +p_payinfo.c:113 1:blrdxp-santbs:CustomerCenter:0:AWT-EventQueue-0:83: +1227256900:0 op_cust_pol_prep_payinfo input flist # number of field entries allocated 20, used 5 0 PIN_FLD_POID POID [0] 0.0.0.1 /payinfo/invoice -1 0 D Fri Nov 21 14:09:41 2008 TME_BILLING_COE cm:10570 fm_cust_pol_val +id_payinfo.c:276 1:blrdxp-santbs:CustomerCenter:0:AWT-EventQueue-0:83 +:1227256900:0 op_cust_pol_valid_payinfo input flist # number of field entries allocated 20, used 8 0 PIN_FLD_ACCOUNT_OBJ POID [0] 0.0.0.1 /account -1 0 0 PIN_FLD_INVOICE_OBJ POID [0] 0.0.0.1 /invoice -1 0 0 PIN_FLD_PAYMENT_OFFSET INT [0] -1 1 PIN_FLD_ADDRESS STR [0] "123\t\t\t" D Fri Nov 21 14:09:41 2008 TME_BILLING_COE cm:10570 fm_cust_pol_val +id_payinfo.c:2569 1:blrdxp-santbs:CustomerCenter:0:AWT-EventQueue-0:8 +3:1227256900:0 Payment term not defined in the inut flist, there is nothing t +o validate # number of field entries allocated 20, used 8 0 PIN_FLD_ACCOUNT_OBJ POID [0] 0.0.0.1 /account -1 0

Replies are listed 'Best First'.
Re: searching for string & number
by brsaravan (Scribe) on Nov 27, 2008 at 09:05 UTC
    Read the log file and use regex to get the pid and the string (.c file )
    open (FH, "logfile") || die "Cannot open file"; while ($line = <FH>) { my ($pid, $cfile) = ($line =~ /cm:(\d+)\s+([^(]*)/); print "$pid $cfile\n"; }
Re: searching for string & number
by prasadbabu (Prior) on Nov 27, 2008 at 09:11 UTC

    Hi jaggu_bg,

    After seeing your code with some assumptions, I have created a below script. You have to be very specific with your input and required output, so that we can give the answer without any assumptions.

    I have taken input file and input string as input parameters and created the output in output.txt file.

    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 matc +hed string print "Usage: test.pl <input file name> <input process id> <input file + string>" 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 ou +tput.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 i +f 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

    Updated:Based on the input given by OP in private message and below reply

    Prasad

      hi, i am new ... sorry for the in adequate data. actually i have to enter a pid number and a string. like 10570 and fm_cust_validate_customer.c. in output i have to get all the lines which matches this two . as u can see in log file it starts with E,D,M,W. i need all lines from one E to other E or D anything which matches pid number & the string.
Re: searching for string & number
by Crian (Deacon) on Nov 27, 2008 at 08:40 UTC

    It would be helpfull to see, what type of result the program should give, what you tried so far and what exactly is going wrong.

      i wanna search for the PID i give and the string.
      #!/tools/opt/bin/t2.perl use Getopt::Std; getopts('hDp:f:sb:e:q:t:jl'); if($Getopt::Std::opt_f) {$sFile = $Getopt::Std::opt_f;} if($Getopt::Std::opt_p) {$iPid = $Getopt::Std::opt_p;} if($Getopt::Std::opt_q) {$sOutFile = $Getopt::Std::opt_q;} if($Getopt::Std::opt_c) {$iStr = $Getopt::Std::opt_c;} my $sAppNameString = "[c,C][m,M]"; my $sStartString = ".?[DWEM]"; my $sAppPidString = ".*\\s+$sAppNameString:(\\d+\\.?\\d*)\\s+.*"; my $sAppString = "$sStartString$sAppPidString"; open(HFILE, "< $sFile") or die ("Failed to open $sFile"); $hFile = *HFILE; print ("Processing file: \"$sFile\"\n"); #Splitting the log file per pid if ($Getopt::Std::opt_s) { seek($hFile,0,0); while (<$hFile>) { if (m/$sAppString/) { $iPidTmp = $1; } if(($iPid != 0) && ($iPid != $iPidTmp)) { next; } $hOutputFile = $hFiles{$iPidTmp}; if($hOutputFile) { print $hOutputFile "$_"; }else{ print ("PID = $iPidTmp\n"); open( HPIDFILE.$iPidTmp, ">$sOutFile.$iPidTmp" ) or die ("Failed to open $sOutputFile.$iPidTmp"); $hFiles{$iPidTmp} = *HPIDFILE.$iPidTmp; $hOutputFile = $hFiles{$iPidTmp}; print $hOutputFile "$_"; } } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://726337]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2023-01-27 20:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?