0

我有一个像

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/Sed 中完成吗?怎么做?

4

2 回答 2

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

于 2012-06-27T20:41:24.527 回答
0

一种使用方式perl

假设infile您的问题内容和下一个内容位于script.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
于 2012-06-27T18:05:10.213 回答