Hi, I am trying to grep a particular pid(process ID) and calculate the time taken by it from a log file.

Sample Log file:
2015/01/15 11:20:01.175-05:00 14788 auser *sync_cmd 7f1fa0c08700 10.101.17.111
2015/01/15 11:26:01.175-05:00 14788 auser sync_cmd 7f1fa0c08700 10.101.17.111

2015/01/15 11:30:01.175-05:00 14790 auser *sync_cmd 7f1fa0c08700 10.101.17.111
2015/01/15 11:36:01.175-05:00 14790 auser sync_cmd 7f1fa0c08700 10.101.17.111

How can I grep the process Id in a log file with particular user,cmd and date?
open(my $fh,"<","/pathtologfile/logfile.log") or die "can not open the + file $!"; my @lines = <$fh>; close($fh); my $count=0; for(my $i=0; $i<=$#lines;$i++) { my @pids = (`ps -ef | grep acuser | grep 2015/01/15 | grep sync_c +md | awk '{print $2}'`) ; }

Original node content restored below by GrandFather

Hi, I am trying to grep a particular pid(process ID) and calculate the time taken by it from a log file.

Sample Log file:
2015/01/15 11:20:01.175-05:00 14788 auser *sync_cmd 7f1fa0c08700 10.101.17.111
2015/01/15 11:26:01.175-05:00 14788 auser sync_cmd 7f1fa0c08700 10.101.17.111

2015/01/15 11:30:01.175-05:00 14790 auser *sync_cmd 7f1fa0c08700 10.101.17.111
2015/01/15 11:36:01.175-05:00 14790 auser sync_cmd 7f1fa0c08700 10.101.17.111

I am asking user to enter user name (auser) , cmd (sync_cmd) and date (2015/01/15). With these 3 parameters ,
1. I would like to grep all the process ids(14788,14790) matching the 3 parameters in an array
2. Loop through array and get time difference between *sync_cmd (start time) and sync_cmd (end time) lines from log file ('2015/01/15 11:26:01.175-05:00' - '2015/01/15 11:20:01.175-05:00'

Final Output Expected
Username : auser
cmd : sync_cmd
Date : 2015/01/15

Process ID : 14788
Start Time : 2015/01/15 11:20:01.175-05:00
End Time : 2015/01/15 11:26:01.175-05:00
Time Taken: 6 mins 20 secs

Process ID : 14790
Start Time : 2015/01/15 11:30:01.175-05:00
End Time : 2015/01/15 11:35:01.175-05:00
Time Taken: 5 mins 10 secs

Here is the code I have. How can I grep the process Id and store it an array and calculate the time taken by it?
#!/usr/bin/perl use strict; use warnings; use Data::Dump qw/ddx/; my ($username,$cmd,$date,$option); sub main(){ system("clear"); system ("cls"); print "************************************************\n"; print "Welcome to Log Grep Tool\n"; print "*************************************************\n"; print "PRESS 1 to grep or any other key to EXIT :"; chomp($option=<STDIN>); if ($option =="1") { getParameters($username,$cmd,$date); viewResults(); } else { print "Goodbye!\n"; exit; } } sub getParameters(){ print "Enter the Username (Eg:auser):" ; chomp($username=<STDIN>); print "Enter the cmd (Eg:sync_cmd):" ; chomp($cmd=<STDIN>); print "Enter the date (Eg:2015/01/14):" ; chomp($date=<STDIN>); return ($username,$cmd,$date); } sub viewResults(){ my @process_ids = (`cat /mnt/accurev_d1/site_slice/logs/acserver.l +og | grep $username | grep $date | grep $cmd | more`) ; foreach (@pids) { print "$_\n"; } } main();

In reply to Perl grep the process id and calculate time taken by sravs448

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.