Multidimensional arrays Shell Programming and Scripting

I have two files:

file-1 is a list of number of interfaces in the switch and file-2 have VLAN-ID , VLAN-NAME , Interface belong to that VLAN like this:

file-1:

1/1
1/2
1/3
1/4
1/5
.
.

file-2:

1,"vlan-wifi",1/1,1/7,1/8
2,"vlan-admin",1/3,1/5/1/6
3," " ,3/3,1/3

I'm trying to use these files to create table:

VLAN-ID | VLAN-NAME |1/1|1/2|1/3|1/4|1/5|1/6|1/7|1/8|1/9|1/10|.....
    1   |  vlan-wifi| * |   |   |   |   |   | * | * |   |    |
    2   | vlan-admin|   |   | * |   | * | * |   |   |   |    |


How can I achieve this?

Welcome to the forum.

Any attempts / ideas / thoughts from your side?

And, what's happening to VLAN-ID 3?

Thank you RudiC

i m full in networking i work so much with CISCO and Juniper so i m new doing automation with Linux i try first to get all the information now i would like some help to manipulate the information with shell script

for the VLAN 3 we have like 4057 VLANS that mean ( 4057 Line ) so the same for as VLAN 1 and VLAN 2 VLAN 3 ..... VLAN 4057

I'm sure you can do what you want in awk. But I don't understand precisely what you want since file-1 doesn't appear to contribute anything to your output.

Hi Corona688

i put in File-1 all the numbers of all the ports in switch ( some switch have different numbers ( example switch-1 have 1/1 ,1/2 ,1/3 ,....... 6/7
and switch-2 have 1/1/1,1/1/2,1/1/3 ............,2/2/17) so this all port numbers i will put them in table

But they're also in file-2. What is it for then?

in File-2 have VLAN-ID VLAN-NAME and ports belong to that VLAN

Example

VLAN-ID : 1 VLAN-NAME : "vlan-wifi" ports belong to that VLAN 1/1,1/7,1/8

1,"vlan-wifi",1/1,1/7,1/8

awk '
NR==FNR {col[NR]=$0; cc=NR; cols[NR]=length($0); a[$0]=$0; next}
NR!=FNR && ! c++ {
   printf ("%-12s|%-12s", "VAN-ID", "VLAN-NAME");
   for (i=1 ; i<=NR; i++) {
      colw=length(col);
      printf ("%s%s", ((i<NR) ? "|" : "\n"), col);
   }
}
{
   sub("^[ \"]*", "", $2);
   sub("[ \"]*$", "", $2);
   if ($2) {
      for (i in aa) delete aa;
      for (i in dd) delete dd;
      for (i=3; i<=NF; i++) if ($i in a) {aa[$i]=" * "}
      for (i=1; i<=cc; i++) {if (col in aa) {dd=" * "}}
      printf ("%12s|%-12s|"), $1, $2;
      for (i=1; i<=cc; i++) printf("%*s%s", cols, dd, ((i<cc) ? "|" : "\n"));
   }
}
' file-1 FS=, file-2

OK, I think I get it.

awk -F"," -v OFS="|" 'NR==FNR { C[$1] ; next } # Save list of ports

{
        ID[$1]  # Save list of IDs
        D[$1,"NAME"]=$2
        for(N=3; N<=NF; N++)    D[$1,$N]
}

END {   for(X in ID) # Print everything
        {
                $0=""; $1=X ; $2=D[X,"NAME"];
                L=3;
                for(V in C)
                {
                        if ((X,V) in D) $L="*";
                        L++
                }
                print;
        }
}' file-1 file-2

Try also

awk -F, '
FNR == NR       {PNR[$1]  = MAXP = NR
                 PORt[NR] = $1
                 next
                }
FNR == 1        {printf "VLAN-ID |  VLAN-NAME "
                 for (i=1; i<=MAXP; i++) printf "|%s", PORt
                 printf RS
                }
                {gsub (/"/, _, $2)
                 printf "%7d |%11s ", $1, $2
                 split (sprintf ("%*s", MAXP, " "), T, _)
                 for (i=3; i<=NF; i++) T[PNR[$i]] = "*"
                 for (i=1; i<=MAXP; i++) printf "| %c ", T
                 printf RS
                }

' file1 file2
VLAN-ID |  VLAN-NAME |1/1|1/2|1/3|1/4|1/5|1/6|1/7|1/8
      1 |  vlan-wifi | * |   |   |   |   |   | * | * 
      2 | vlan-admin |   |   | * |   | * | * |   |   
      3 |            |   |   | * |   |   |   |   |