1

I am trying to put this output in columns can anyone help?

Routeur 2 ==> 89.89.156.112 If_Name
:vRtrIfName.1.1
:vRtrIfName.1.2
Routeur 2 ==> 89.89.156.112 If_index
546
789
Routeur 2 ==> 89.89.156.112 If_status
1
2

Designed output:

routerName  IPadd          If_Name           If_index    If_status

Routeur 2   89.89.156.112 :vRtrIfName.1.1    546         1
Routeur 2   89.89.156.112 :vRtrIfName.1.2    789         2
  • How are you trying to obtain the output? – Braiam Apr 23 '15 at 13:07
1

Perl solution, using Text::Table for nice output formatting:

#!/usr/bin/perl
use warnings;
use strict;

use Text::Table;

my %t;
my ($router, $ip, $column);
while (<>) {
    if (/==>/) {
        ($router, $ip, $column) = /(.*) ==> ([0-9.]+) (\S+)/;
    } else {
        chomp;
        push @{ $t{$router}{$ip}{$column} }, $_;
    }
}

my @columns = qw( If_Name If_index If_status );
my $tt = 'Text::Table'->new('routerName', 'IPadd', @columns);
for my $router (keys %t) {
    for my $ip (keys %{ $t{$router} }) {
        for my $i (0 .. $#{ $t{$router}{$ip}{ $columns[0] } }) {
            my @values = map $t{$router}{$ip}{$_}[$i], @columns;
            $tt->add($router, $ip, @values);
        }
    }
}

print $tt;
0

Awk solution

awk '
/Routeur/{
    i=0;
    host[$1" "$2]=$4;
    k++;
    param[k]=$5;
    next }
{
    i++
    Data[k,i]=$0
}
END{ 
    printf("%-12s%-15s","routerName","IPadd");
    for(j in param)
        printf("%-17s",param[j]);
    print "\n";
    for(h in host)
        for(i=1;i<3;i++){
            printf("%-12s%-15s",h,host[h]);
            for(j in param)
                printf "%-17s", Data[j,i];
            print""     }
   }'

For different hosts should be modified according to number of fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.