perl regex从top命令的输出中提取数据

时间:2014-06-13 07:35:02

标签: regex perl

以下是示例顶部命令输出

top - 12:57:37 up 6 days, 17:04,  9 users,  load average: 2.06, 1.44, 1.18
Tasks: 192 total,   3 running, 189 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.9 us,  0.4 sy,  0.0 ni, 97.6 id,  1.1 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   3893176 total,  3361060 used,   532116 free,   908568 buffers
KiB Swap:  4035580 total,  1427744 used,  2607836 free,   571052 cached

  PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND
   35 root      20   0     0    0    0 S  13.0  0.0   6:57.39 kswapd0
 2196 venkat    20   0 1943m 102m  12m S   6.5  2.7 115:10.61 cinnamon
 2258 venkat    20   0 1313m  73m  10m R   6.5  1.9  16:02.05 nemo
25761 root      20   0     0    0    0 S   6.5  0.0   0:09.86 ksoftirqd/2
    1 root      20   0 24448 1840 1084 S   0.0  0.0   0:01.06 init

我想提取VIRT RES SHR数据,我已经尝试了以下正则表达式提取

if($_=~/\d+\s+\w+\s+\d+\s+\d+\s+([\d\.mgk]*)\s+([\d\.mgk]*)\s+([\d\.mgk]*)\s+\S+\s+(\S+)\s+(\S+)/)
   {
        my $virtual=$1;
        my $resident=$2;
        my $shared=$3;
   }

这种情况并不令人满意

3 个答案:

答案 0 :(得分:3)

不需要复杂的正则表达式和列数据。只需使用split

跳到标题行,然后每行只有12列:

use strict;
use warnings;

while (<DATA>) {
    next if 1 .. /PID/;      # Skip until header row

    my ($vert, $res, $shr) = (split ' ', $_, 12)[4,5,6];

    printf "% 5s % 5s % 5s\n", $vert, $res, $shr;
}

__DATA__
top - 12:57:37 up 6 days, 17:04,  9 users,  load average: 2.06, 1.44, 1.18
Tasks: 192 total,   3 running, 189 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.9 us,  0.4 sy,  0.0 ni, 97.6 id,  1.1 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   3893176 total,  3361060 used,   532116 free,   908568 buffers
KiB Swap:  4035580 total,  1427744 used,  2607836 free,   571052 cached

  PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND
   35 root      20   0     0    0    0 S  13.0  0.0   6:57.39 kswapd0
 2196 venkat    20   0 1943m 102m  12m S   6.5  2.7 115:10.61 cinnamon
 2258 venkat    20   0 1313m  73m  10m R   6.5  1.9  16:02.05 nemo
25761 root      20   0     0    0    0 S   6.5  0.0   0:09.86 ksoftirqd/2
    1 root      20   0 24448 1840 1084 S   0.0  0.0   0:01.06 init

输出:

    0     0     0
1943m  102m   12m
1313m   73m   10m
    0     0     0
24448  1840  1084

答案 1 :(得分:2)

使用GTop模块,可以使用所需的方法。

use GTop ();
my $gtop = GTop->new;
my $proc_mem = $gtop->proc_mem($pid);
my $vsize = $proc_mem->vsize; #VIRT
my $resident = $proc_mem->resident; #RES
my $share = $proc_mem->share; #SHR

答案 2 :(得分:1)

根据您打算对此数据执行的操作,构建包含每个进程的所有数据的哈希可能很有用。该计划展示了一种方法。

哈希的键被剥去非字母字符并转换为小写字母。

use strict;
use warnings;

my (@keys, @top);

for (`top -bn1`) {
  if (@keys) {
    my %data;
    @data{@keys} = split;
    push @top, \%data;
  }
  elsif ( /^\s*PID/ ) {
    @keys = map { (my $key = lc) =~ tr/a-z//dc; $key; } split;
  }
}

for my $item (@top) {
  printf "%6d: %6d %6d %6d\n", @{$item}{qw/ pid virt res shr /};
}

<强>输出

rob@Samurai-U:~/perl/source$ perl top.pl
  2788:  24852   1488   1084
   887:  19188    712    492
  1571: 1159076  11992   6688
     1:  33908   3140   1408
     2:      0      0      0
     3:      0      0      0
     4:      0      0      0
     5:      0      0      0
     6:      0      0      0
     7:      0      0      0
     8:      0      0      0
     9:      0      0      0
    10:      0      0      0
    11:      0      0      0
    12:      0      0      0
    13:      0      0      0
    14:      0      0      0
    15:      0      0      0
    16:      0      0      0
    17:      0      0      0
    18:      0      0      0
    19:      0      0      0
    20:      0      0      0
    21:      0      0      0
    22:      0      0      0
    23:      0      0      0
    24:      0      0      0
    25:      0      0      0
    26:      0      0      0
    27:      0      0      0
    28:      0      0      0
    29:      0      0      0
    30:      0      0      0
    31:      0      0      0
    32:      0      0      0
    33:      0      0      0
    34:      0      0      0
    35:      0      0      0
    36:      0      0      0
    37:      0      0      0
    38:      0      0      0
    39:      0      0      0
    40:      0      0      0
    52:      0      0      0
    53:      0      0      0
    54:      0      0      0
    55:      0      0      0
    76:      0      0      0
    77:      0      0      0
   124:      0      0      0
   126:      0      0      0
   136:      0      0      0
   137:      0      0      0
   138:      0      0      0
   296:  19608    912    584
   304:  51696   1808    980
   370:      0      0      0
   385:  40304   2476    948
   413:  19292   1432   1168
   420:  43452   1800   1428
   444:      0      0      0
   446: 255840   1576    864
   464:  32352   1672   1372
   465:  32224    468    220
   582: 330236   4296   3176
   629: 340696   5964   4544
   638: 281344   5164   2980
   656:  15612   1064    444
   661:  15392    736    332
   679:  10232   3648   1356
   736:  15820    936    776
   740:  15820    940    776
   746:  15820    936    776
   747:  15820    928    776
   750:  15820    940    776
   813:  37144   1008    688
   821:  23656   1004    760
   833:   4368    680    508
   835:  31032   1496   1244
   845: 361300   7124   3712
   848: 277852   3608   2824
   879: 287480   4212   3208
   972: 528612 116180  34192
   999:   4444    648    544
  1019:  15588   1320    852
  1073:  75352   3364   2468
  1089: 168296   3584   2820
  1090:      0      0      0
  1100:  36128   2576   1536
  1185: 216604   1092    756
  1249:  15820    936    776
  1340: 109444   1764   1232
  1348: 111644   1688   1080
  1352:  41852   1208    764
  1358: 108168   1448    896
  1380:  10616    316      0
  1386:  41084   2524    840
  1395:  18108   1144    968
  1400:  74004   4356   3804
  1414:  26596    700    404
  1416:  18116    416    196
  1418:  18116    640    388
  1420: 358428   5196   2868
  1436: 192484   3120   2508
  1438: 735528  22140  11596
  1447: 644036  25276  15424
  1450: 337592   3324   2784
  1451: 571716  11400   7744
  1456:  39364   1988   1412
  1458: 500532  24064  11108
  1462: 345664   3120   2480
  1465: 276696   3432   2808
  1466: 476552  17008  10148
  1468: 382692   7400   5696
  1488: 546820  12496   7800
  1493: 125008   3320   2728
  1526: 239368   4372   3456
  1551: 584200  14888   7688
  1555: 330932   2972   2472
  1563: 259488   2856   2372
  1568: 274268   3312   2660
  1570: 219272   4036   3032
  1577: 475236   5992   4308
  1583: 442592  13600   7976
  1590: 889572   3848   2888
  1609: 286748   4972   3888
  1639: 369488   5952   3848
  1655: 476748  10492   7876
  1661: 168916   1276   1052
  1680: 200968   3268   2688
  1755: 336308  10156   7084
  1759: 1344936  78360  37228
  1765: 1069452  46096   7848
  1796: 399124   9400   6532
  1797: 301548   5712   4268
  1799: 178308   2708   2088
  1807: 888740  32244  23772
  1809: 599268  17924  12412
  1814: 327668   9672   6776
  1878: 294680   6828   3368
  1884: 371396   4904   3304
  1934:  53964   3544   2072
  1938: 208256   3268   2580
  1942: 281808   3320   2672
  1947: 196092   4760   2200
  1955: 428980   3764   2972
  1980: 266216   5028   2440
  1997: 1421080  81428  28032
  2013: 186656  11868   2620
  2041: 451068  12176   8916
  2053: 322340   7156   5724
  2075: 467348   9220   5420
  2080: 331276   3748   2944
  2087: 233192   5520   4504
  2099:   7216    360    280
  2168: 493812  13500   9744
  2190: 650960 104788  55892
  2219: 646848  19148  12576
  2226:  14824    792    640
  2227:  22720   3820   1696
  2278: 447316   5976   3136
  2288:  20808   2396   1740
  2289:  24984   1672   1176
  2290:  24984   1672   1176
  2632:  76856   3364   2356
  2783:      0      0      0
  2787:  20808   2268   1692