2012-06-27 54 views
0

的面積我有一個像AWK聯合收割機基於文件

Sever Name aad98722RHEL 20120630 075022 

CPU 
1 sec 10 sec 15 sec 1 min 1 hour 
5  8  0  1  19 

TX kbits/sec: 

interface 10 sec  1 min  10 min  1 hour  1 day 
--------- ------  -----  ------  ------  ----- 
eth0  32   33   39   40   33 
eth1  6   186   321   199   18 
eth2  0   0   0   0   0 
mgt0  0   0   0   0   0 

RX kbits/sec: 

interface 10 sec  1 min  10 min  1 hour  1 day 
--------- ------  -----  ------  ------  ----- 
eth0  19   19   25   26   23 
eth1  9   26   40   28   10 
eth2  0   0   0   0   0 
mgt0  0   0   0   0   0 

Total memory usage: 1412916 kB 
Resident set size : 1256360 kB 
Heap usage  : 1368212 kB 
Stack usage  : 84 kB 
Library size  : 16316 kB 

一個文件,我想生產什麼是

aad98722RHE 20120630 075022 CPU 5  8  0  1  19 
aad98722RHE 20120630 075022 TX kbits/sec: 32   33   39   40   33  6   186   321   199  18  0   0   0   0   0  0   0   0   0   0 
aad98722RHE 20120630 075022 RX kbits/sec: 19   19   25   26   23  9   26   40   28  10  0   0   0   0   0  0   0   0   0   0 
aad98722RHE 20120630 075022 Total memory usage: 1412916 kB Resident set size : 1256360 kB Heap usage  : 1368212 kB Stack usage  : 84 kB Library size  : 16316 kB 

可這在在awk /桑達和如何做?使用perl

+2

'yes'和'與一些effort' –

回答

0

方式一:

假設infilescript.pl你的問題和未來內容的內容:

use warnings; 
use strict; 

my ($header, $newlines, $trans, @nums); 

## Read input in paragraph mode. 
local $/ = qq||; 

while (my $par = <>) { 
    chomp $par; 

    ## Save data of the header. 
    if ($. == 1) { 
     my @header = $par =~ m/\ASer?ver\s+Name\s+(\S+)\s+(\S+)\s+(\S+)\s*\Z/s; 
     last unless @header; 
     $header = join qq| |, @header; 
     next; 
    } 

    ## Number of '\n' in each paragraph (number of lines minus one). 
    $newlines = $par =~ tr/\n/\n/; 

    ## Three lines, the CPU info. Extract what I need and print. 
    if ($newlines == 2) { 
     printf qq|%s %s %s\n|, $header, $par =~ m/\A([^\n]+).*\n([^\n]+)\Z/s; 
     next; 
    } 

    ## Transmission string. 
    if ($newlines == 0) { 
     $trans = $par; 
     next; 
    } 

    ## Transmission info. Extract numbers and print. 
    if ($newlines == 5) { 
     my @lines = split /\n/, $par; 
     for my $i (0 .. $#lines) { 
      my @f = split /\s+/, $lines[ $i ]; 
      if (grep { m/\D/ } @f[ 1 .. $#f ]) { 
       next; 
      } 
      else { 
       push @nums, @f[ 1 .. $#f ]; 
      } 
     } 
     printf qq|%s %s\n|, $header, join qq| |, @nums; 
     @nums =(); 
    } 

    ## Resume info. Extract and print. 
    if ($newlines == 4) { 
     $par =~ s/\n/\t/gs; 
     printf qq|%s %s\n|, $header, $par; 
    } 
} 

運行它想:

perl script.pl infile 

有了以下的輸出:

aad98722RHEL 20120630 075022 CPU 5 8 0 1 19 
aad98722RHEL 20120630 075022 32 33 39 40 33 6 186 321 199 18 0 0 0 0 0 0 0 0 0 0 
aad98722RHEL 20120630 075022 19 19 25 26 23 9 26 40 28 10 0 0 0 0 0 0 0 0 0 0 
aad98722RHEL 20120630 075022 Total memory usage: 1412916 kB  Resident set size : 1256360 kB Heap usage : 1368212 kB Stack usage : 84 kB  Library size : 16316 kB 
1

也許它不是更好的解決方案,但它的工作。

文件:a.awk:

function print_cpu(server_name, cpu) 
{ 
    while ($0 !~ cpu)     
    { 
     getline 
    }  

    getline 
    getline 

    printf "%s %s ", server_name, cpu 

    for (i = 1; i < NF + 1; i++) 
    { 
     printf "%s ", $i 
    } 

    printf "\n" 
} 

function print_rx_or_tx(server_name, rx_or_tx) 
{ 
    while ($0 !~ rx_or_tx) 
    { 
     getline  
    } 

    getline 
    getline 
    getline 

    printf "%s %s ", server_name, rx_or_tx 

    while ($0 != "") 
    { 
     getline 

     for (i = 2; i < NF; i++) 
     { 
      printf "%s ", $i 
     }  
    } 

    printf "\n" 
} 

function print_stuff(server_name) 
{ 
    while ($0 == "") 
    { 
     getline  
    } 

    printf "%s ", server_name 

    while ($0 != "") 
    { 
     printf "%s ", $0 

     if (getline <= 0) 
     { 
      break  
     } 
    } 

    printf "\n" 
} 

BEGIN { server = "Server Name"; cpu = "CPU"; tx = "TX kbits/sec:"; rx = "RX kbits/sec:" } 

server { server_name = $3 " " $4 " " $5 } 

! server 
{  
    print_cpu(server_name, cpu) 
    print_rx_or_tx(server_name, tx ) 
    print_rx_or_tx(server_name, rx ) 
    print_stuff(server_name) 
} 

運行:AWK -f a.awk your_input_file