formatando dados no shell script

0
cat <file1>
name=HOST2
WWN=50.01.43.80.29.6A.84.82
WWN=50.01.43.80.29.6A.84.89
WWN=50.01.43.80.29.6A.84.8A
objectID=LU.R800.57488.43
objectID=LU.R800.57488.44
objectID=LU.R800.57489.44
name=HOST3
WWN=50.01.43.80.28.55.99.5C
WWN=50.01.43.80.28.55.99.5D
objectID=LU.R800.57488.45
objectID=LU.R800.57488.46
objectID=LU.R800.57488.47
objectID=LU.R800.5748A.47
name=HOST2
WWN=50.01.43.80.29.6A.84.87
WWN=50.01.43.80.29.6A.84.88
objectID=LU.R800.57486.41
objectID=LU.R800.57486.42
objectID=LU.R800.57486.43

Eu preciso que a saída seja assim. alguém pode ajudar no shell

HOST2 50.01.43.80.29.6A.84.82 LU.R800.57488.43
HOST2 50.01.43.80.29.6A.84.82 LU.R800.57488.44
HOST2 50.01.43.80.29.6A.84.82 LU.R800.57489.44
HOST2 50.01.43.80.29.6A.84.89 LU.R800.57488.43
HOST2 50.01.43.80.29.6A.84.89 LU.R800.57488.44
HOST2 50.01.43.80.29.6A.84.89 LU.R800.57489.44
HOST2 50.01.43.80.29.6A.84.8A LU.R800.57488.43
HOST2 50.01.43.80.29.6A.84.8A LU.R800.57488.44
HOST2 50.01.43.80.29.6A.84.8A LU.R800.57489.44
HOST3 50.01.43.80.28.55.99.5C LU.R800.57488.45
HOST3 50.01.43.80.28.55.99.5C LU.R800.57488.46
HOST3 50.01.43.80.28.55.99.5C LU.R800.57488.47
HOST3 50.01.43.80.28.55.99.5C LU.R800.5748A.47
HOST3 50.01.43.80.28.55.99.5D LU.R800.57488.45
HOST3 50.01.43.80.28.55.99.5D LU.R800.57488.46
HOST3 50.01.43.80.28.55.99.5D LU.R800.57488.47
HOST3 50.01.43.80.28.55.99.5D LU.R800.5748A.47
HOST2 50.01.43.80.29.6A.84.87 LU.R800.57486.41
HOST2 50.01.43.80.29.6A.84.87 LU.R800.57486.42
HOST2 50.01.43.80.29.6A.84.87 LU.R800.57486.43
HOST2 50.01.43.80.29.6A.84.88 LU.R800.57486.41
HOST2 50.01.43.80.29.6A.84.88 LU.R800.57486.42
HOST2 50.01.43.80.29.6A.84.88 LU.R800.57486.43
    
por Anesh Kumar 15.03.2017 / 14:01

2 respostas

0

Aqui está um script perl simples:

#!/usr/bin/perl -n
#

sub printall {
    foreach my $wwn (@wwns) {
        foreach my $obj (@objects) {
            print "$name $wwn $obj\n";
        }
    }
}

if (/name=(.*)/) {
    if ($name) {
        printall;
    }
    $name = $1;
    @wwns = ();
    @objects = ();
} elsif (/WWN=(.*)/) {
    push @wwns, $1;
} elsif (/objectID=(.*)/) {
    push @objects, $1;
}

END {
    printall;
}

Faz um loop sobre a linha em um arquivo, coletando as WWN s e objectId s. Então, toda vez que encontrar uma linha com name= ou o final do arquivo, ele imprime o que foi coletado, na ordem do seu exemplo.

    
por 15.03.2017 / 14:32
0

awk versão:

#!/usr/bin/awk -f

function output(name,WWN,objectID) {
  for (x in WWN) {
    for (y in objectID) {
      print name, x, y;
    }
  }
}

BEGIN {
  FS = "=";
}

{
  if ($1=="name") {
    output(name,WWN,objectID);
    name = $2;
    delete WWN;
    delete objectID;
  }
  if ($1=="WWN") {WWN[$2]=$2}
  if ($1=="objectID") {objectID[$2]=$2}
}

END {output(name,WWN,objectID)}
    
por 15.03.2017 / 15:32