CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Extract from a file

 



perlwatcher
User

Jun 8, 2010, 8:09 AM

Post #1 of 3 (3778 views)
Extract from a file Can't Post

I have an output in one singular scalar

the following is the output


Code
 
IPP CPU Utilization Slot 3:

top - 08:03:09 up 1 day, 3:17, 0 users, load average: 0.12, 0.10, 0.02
Tasks: 74 total, 1 running, 73 sleeping, 0 stopped, 0 zombie
Cpu(s): 1.1% us, 1.8% sy, 0.0% ni, 94.5% id, 0.0% wa, 0.8% hi, 1.8% si
Mem: 3024960k total, 498344k used, 2526616k free, 7852k buffers
Swap: 0k total, 0k used, 0k free, 235656k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3721 root 16 0 99.2m 46m 1608 S 0.0 1.6 4:13.64 aggregator
3699 root 15 0 54608 47m 1784 S 0.0 1.6 0:55.98 nsm
3726 root 15 0 72796 34m 2576 S 0.0 1.2 0:51.29 msc
3706 root 15 0 31008 23m 1564 S 0.0 0.8 0:12.65 qos_mgr
3734 root 15 0 25384 19m 2952 S 0.0 0.7 0:01.09 had
3738 root 15 0 56676 18m 1540 S 0.0 0.6 3:46.42 authd
3736 root 15 0 20392 15m 1400 S 0.0 0.5 0:42.94 acctd
2942 root 15 0 96468 7712 3204 S 0.0 0.3 5:11.58 WIPPd
3769 root 15 0 44100 6268 1352 S 0.0 0.2 0:54.13 sfa
3733 root 15 0 11536 6932 1320 S 0.0 0.2 0:01.68 wi_dhcp


IPP CPU Utilization Slot 4:

top - 08:03:09 up 1 day, 3:14, 0 users, load average: 0.04, 0.06, 0.05
Tasks: 75 total, 1 running, 73 sleeping, 0 stopped, 1 zombie
Cpu(s): 0.9% us, 1.5% sy, 0.0% ni, 95.0% id, 0.0% wa, 0.8% hi, 1.7% si
Mem: 3024960k total, 320540k used, 2704420k free, 7724k buffers
Swap: 0k total, 0k used, 0k free, 152892k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3729 root 16 0 26344 3276 2132 S 3.9 0.1 24:02.26 fm_clnt
3698 root 15 0 54608 47m 1776 S 0.0 1.6 0:54.67 nsm
3730 root 15 0 30704 23m 1480 S 0.0 0.8 0:01.25 qos_mgr
3750 root 15 0 49996 12m 1744 S 0.0 0.4 0:02.17 msc
2942 root 15 0 96460 7708 3204 S 0.0 0.3 0:43.70 WIPPd
3760 root 15 0 12784 8304 1400 S 0.0 0.3 0:01.40 acctd
3762 root 15 0 44924 7596 1536 S 0.0 0.3 0:00.51 authd
3758 root 15 0 14984 9888 2392 S 0.0 0.3 0:00.33 had
3745 root 16 0 60080 6104 1580 S 0.0 0.2 0:04.97 aggregator
3779 root 15 0 44096 6260 1352 S 0.0 0.2 0:01.89 sfa


[\code]


I need to split the above $output in to an array whose elements
doesnt include the following between


Code
 
IPP CPU Utilization Slot 4:

top - 08:03:09 up 1 day, 3:14, 0 users, load average: 0.04, 0.06, 0.05
Tasks: 75 total, 1 running, 73 sleeping, 0 stopped, 1 zombie
Cpu(s): 0.9% us, 1.5% sy, 0.0% ni, 95.0% id, 0.0% wa, 0.8% hi, 1.7% si
Mem: 3024960k total, 320540k used, 2704420k free, 7724k buffers
Swap: 0k total, 0k used, 0k free, 152892k cached

[\code]


Can anyone please help write a regex to remove the above text from the top text.

Thanks,

Arun


Bianca
User

Jun 8, 2010, 9:54 AM

Post #2 of 3 (3772 views)
Re: [perlwatcher] Extract from a file [In reply to] Can't Post

my proposal.
don't know what you want to do with the data.


Code
 #!/usr/bin/perl -W  
use strict;
use warnings;
use diagnostics;

my $string = <<DATA_STRING;
IPP CPU Utilization Slot 3:

top - 08:03:09 up 1 day, 3:17, 0 users, load average: 0.12, 0.10, 0.02
Tasks: 74 total, 1 running, 73 sleeping, 0 stopped, 0 zombie
Cpu(s): 1.1% us, 1.8% sy, 0.0% ni, 94.5% id, 0.0% wa, 0.8% hi, 1.8% si
Mem: 3024960k total, 498344k used, 2526616k free, 7852k buffers
Swap: 0k total, 0k used, 0k free, 235656k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3721 root 16 0 99.2m 46m 1608 S 0.0 1.6 4:13.64 aggregator
3699 root 15 0 54608 47m 1784 S 0.0 1.6 0:55.98 nsm
3726 root 15 0 72796 34m 2576 S 0.0 1.2 0:51.29 msc
3706 root 15 0 31008 23m 1564 S 0.0 0.8 0:12.65 qos_mgr
3734 root 15 0 25384 19m 2952 S 0.0 0.7 0:01.09 had
3738 root 15 0 56676 18m 1540 S 0.0 0.6 3:46.42 authd
3736 root 15 0 20392 15m 1400 S 0.0 0.5 0:42.94 acctd
2942 root 15 0 96468 7712 3204 S 0.0 0.3 5:11.58 WIPPd
3769 root 15 0 44100 6268 1352 S 0.0 0.2 0:54.13 sfa
3733 root 15 0 11536 6932 1320 S 0.0 0.2 0:01.68 wi_dhcp


IPP CPU Utilization Slot 4:

top - 08:03:09 up 1 day, 3:14, 0 users, load average: 0.04, 0.06, 0.05
Tasks: 75 total, 1 running, 73 sleeping, 0 stopped, 1 zombie
Cpu(s): 0.9% us, 1.5% sy, 0.0% ni, 95.0% id, 0.0% wa, 0.8% hi, 1.7% si
Mem: 3024960k total, 320540k used, 2704420k free, 7724k buffers
Swap: 0k total, 0k used, 0k free, 152892k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3729 root 16 0 26344 3276 2132 S 3.9 0.1 24:02.26 fm_clnt
3698 root 15 0 54608 47m 1776 S 0.0 1.6 0:54.67 nsm
3730 root 15 0 30704 23m 1480 S 0.0 0.8 0:01.25 qos_mgr
3750 root 15 0 49996 12m 1744 S 0.0 0.4 0:02.17 msc
2942 root 15 0 96460 7708 3204 S 0.0 0.3 0:43.70 WIPPd
3760 root 15 0 12784 8304 1400 S 0.0 0.3 0:01.40 acctd
3762 root 15 0 44924 7596 1536 S 0.0 0.3 0:00.51 authd
3758 root 15 0 14984 9888 2392 S 0.0 0.3 0:00.33 had
3745 root 16 0 60080 6104 1580 S 0.0 0.2 0:04.97 aggregator
3779 root 15 0 44096 6260 1352 S 0.0 0.2 0:01.89 sfa
DATA_STRING
my @temp = split /\n/,$string;
my $nr = 0;
my %data;
$_ =~ s/[\r\n]// for @temp;
foreach (my $z = 0; $z < scalar @temp; $z ++) {
if ($temp[$z] =~ /IPP CPU Utilization Slot (\d+):/) {
$nr = $1;
}
elsif ($temp[$z] =~ /^[ \d]+/) {
$data{$nr} .= $temp[$z] . "\n";
}
}

# Test output
foreach my $cpunr (sort {$a <=> $b} keys %data) {
print "CPU #$cpunr:\n" . $data{$cpunr};
}



(This post was edited by Bianca on Jun 8, 2010, 9:56 AM)


deepeshtronics
Novice

Jul 30, 2010, 12:33 PM

Post #3 of 3 (3246 views)
Re: [perlwatcher] Extract from a file [In reply to] Can't Post

After having looked at the input pattern, i found the following solution.

Suppose you have the content in the scalar variable $str.

Just write the following code, i hope you will get what you want.

$str =~ s/IPP CPU Utilization.*?cached//sgim;
$str =~ s/(\s*\n\s*)+/\n/g;
@arr = split /\n+/, $str;

print "$_\n" for @arr;

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives