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

Hello all. I am working with the following log file. What I need to do is, for each iteration, grab the date, and for each line with a command of "ns-httpd" grab the PID and Size. Each iteration could have 1-9 instances of "ns-httpd". After I get the information I need, I have to output to a text file in the form of:

Date, PID1, PID1_Size, PID2, PID2_Size,...PID9
12/23, 12222, 300K, 19488, 499K, etc, etc

I don't really even know where to get started, so any suggestions would be greatly appreciated. Part of the logfile i'm working with is as follows:

Thu Dec 19 12:44:34 EST 2002
sleep time=60 seconds
DATE Thu Dec 19 12:44:34 EST 2002
######## Iteration 0 #########

last pid: 18951; load averages: 7.29, 14.09, 16.97 12:44:34
78 processes: 76 sleeping, 1 zombie, 1 on cpu

Memory: 8192M real, 5888M free, 3819M swap in use, 4794M swap free


PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND
18916 root 1 41 0 3536K 2832K sleep 0:00 0.32% sshd
18918 root 1 41 0 3536K 2832K sleep 0:00 0.32% sshd
18919 root 1 48 0 3536K 2832K sleep 0:00 0.32% sshd
18917 root 1 48 0 3536K 2832K sleep 0:00 0.31% sshd
18647 root 149 58 0 470M 214M sleep 5:27 0.24% ns-httpd
17932 root 139 58 0 469M 211M sleep 5:19 0.23% ns-httpd
18626 root 140 58 0 468M 203M sleep 3:29 0.22% ns-httpd
18668 root 159 58 0 469M 199M sleep 2:59 0.20% ns-httpd
18605 root 165 58 0 469M 205M sleep 3:49 0.19% ns-httpd
18710 root 145 58 0 468M 197M sleep 3:04 0.18% ns-httpd
18689 root 131 58 0 468M 199M sleep 3:10 0.17% ns-httpd
18947 root 1 0 0 1792K 1200K cpu/4 0:00 0.13% top
18731 root 144 58 0 468M 199M sleep 3:08 0.13% ns-httpd
744 root 1 58 0 2632K 1376K sleep 0:04 0.02% sshd
18940 root 1 18 0 1840K 1376K sleep 0:00 0.01% processmonitor.
DATE Thu Dec 19 12:44:34 EST 2002
######## Iteration 1 #########

last pid: 18951; load averages: 7.29, 14.09, 16.97 12:44:34
78 processes: 76 sleeping, 1 zombie, 1 on cpu

Memory: 8192M real, 5888M free, 3819M swap in use, 4794M swap free


PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND
18916 root 1 41 0 3536K 2832K sleep 0:00 0.32% sshd
18918 root 1 41 0 3536K 2832K sleep 0:00 0.32% sshd
18919 root 1 48 0 3536K 2832K sleep 0:00 0.32% sshd
18917 root 1 48 0 3536K 2832K sleep 0:00 0.31% sshd
18647 root 149 58 0 470M 214M sleep 5:27 0.24% ns-httpd
17932 root 139 58 0 469M 211M sleep 5:19 0.23% ns-httpd
18626 root 140 58 0 468M 203M sleep 3:29 0.22% ns-httpd
18668 root 159 58 0 469M 199M sleep 2:59 0.20% ns-httpd
18605 root 165 58 0 469M 205M sleep 3:49 0.19% ns-httpd
18710 root 145 58 0 468M 197M sleep 3:04 0.18% ns-httpd
18689 root 131 58 0 468M 199M sleep 3:10 0.17% sshd
18947 root 1 0 0 1792K 1200K cpu/4 0:00 0.13% top
18731 root 144 58 0 468M 199M sleep 3:08 0.13% ns-httpd
744 root 1 58 0 2632K 1376K sleep 0:04 0.02% sshd
18940 root 1 18 0 1840K 1376K sleep 0:00 0.01% processmonitor.
  • Comment on Extracting a whole line from text file when a keyword is found

Replies are listed 'Best First'.
Re: Extracting a whole line from text file when a keyword is found
by jdporter (Paladin) on Dec 23, 2002 at 17:49 UTC
    Assuming the file is too large to bring the whole thing into memory at once, I'd set your input record separator to "\nDATE ", and then each record can be picked apart rather easily.
    use Date::Manip; local $/ = "\nDATE "; while (<>) { my @l = split /\n/; my @out; push @out, UnixDate( shift(@l), "%m/%d" ); # from Date::Manip for ( @l ) { my %r; @r{qw( pid user thr pri nice size res state time cpu comm )} = + split; $r{'comm'} eq 'ns-httpd' and push @out, @r{qw( pid size )}; } local $" = ","; print "@out\n"; }

    jdporter
    ...porque es dificil estar guapo y blanco.