Multidimensional arrays Shell Programming and Scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multidimensional arrays Shell Programming and Scripting
# 8  
Old 08-09-2017
Code:
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[i]=length(col[i]);
      printf ("%s%s", ((i<NR) ? "|" : "\n"), col[i]);
   }
}
{
   sub("^[ \"]*", "", $2);
   sub("[ \"]*$", "", $2);
   if ($2) {
      for (i in aa) delete aa[i];
      for (i in dd) delete dd[i];
      for (i=3; i<=NF; i++) if ($i in a) {aa[$i]=" * "}
      for (i=1; i<=cc; i++) {if (col[i] in aa) {dd[i]=" * "}}
      printf ("%12s|%-12s|"), $1, $2;
      for (i=1; i<=cc; i++) printf("%*s%s", cols[i], dd[i], ((i<cc) ? "|" : "\n"));
   }
}
' file-1 FS=, file-2


Last edited by rdrtx1; 08-09-2017 at 03:39 PM..
# 9  
Old 08-09-2017
OK, I think I get it.

Code:
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

# 10  
Old 08-09-2017
Try also
Code:
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[i]
                 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[i]
                 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 |            |   |   | * |   |   |   |   |

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

2. Shell Programming and Scripting

Multidimensional arrays

Hi Experts, I'm a newbie.....multidimensional arrays in shell scripts is possible??? If so, can anyone guide me with an example.....and also can anyone tell me, how we can create a table like ouput format in shell script....the output should look like this one: 1 2 3 4 5 6 6 5 4 3... (4 Replies)
Discussion started by: kritibalu
4 Replies

3. Programming

Multidimensional arrays and sort.

My language is C++. I have a multidimensional vector that I would like to sort by a specific "cell" or "field" within the main vector. Does anyone have any information on how to do this? I have searched all over the internet and every reference manual I can find. So far I have found very little to... (2 Replies)
Discussion started by: sepoto
2 Replies

4. Shell Programming and Scripting

multidimensional arrays using awk

i'm trying to use awk to count a listing similar to the following and get a report of the listing similar to the one below it. y,pizza n,pizza y,pizza y,pizza n,tomato n,tomato y,cheese y,cheese n,cheese report ---- pizza,3,1 tomato,0,2 cheese,2,1 (1 Reply)
Discussion started by: multimulti
1 Replies

5. Programming

C programming working with multidimensional array

Hi, I have the following variable declaration which looks like a 3d array or N matrixs KxK of floats float (*table); I have to pass to a function only the first table. How can I do it?? Thanks (6 Replies)
Discussion started by: littleboyblu
6 Replies

6. UNIX for Dummies Questions & Answers

Concatenating arrays cell values in shell scripting

Hi All, I want to concatenate the array cell values and form a string.. Is it possible? for ex. I have an array word_array contains d u m b and after concatenating the string shld be 'dumb' thanks (2 Replies)
Discussion started by: mathur
2 Replies

7. Shell Programming and Scripting

Shell Programming and Scripting

Hi, Iam having the files as follows: file1 aa aa aa aa ab ac ad ae file2 aa aa ab Outputfile: (20 Replies)
Discussion started by: nivas
20 Replies

8. Shell Programming and Scripting

Shell Programming and Scripting

I want to compare some files. say iam having 2 sets of files ,each is having some 10 files. ie, file1 1a.txt 1b.txt 1c.txt ... file2 2a.txt 2b.txt 2c.txt ... i need to read line by line of this files parralley.. ie.. i want to read file1 first line that is 1a.txt and file2... (2 Replies)
Discussion started by: nivas
2 Replies

9. Shell Programming and Scripting

Shell Programming and Scripting

Hi, iam having the file as follows: ABCDEFGH|0987654321234567 ABCDEFGH|0987654321234523 ABCDEFGH|0987654321234556 ABCDEFGH|0987654321234545 POIUYTRE|1234567890890678 POIUYTRE|1209867757352567 POIUYTRE|5463879088797131 POIUYTRE|5468980091344456 pls provide me the split command ... (14 Replies)
Discussion started by: nivas
14 Replies

10. Shell Programming and Scripting

multidimensional arrays

I have a file that's logically in the form of a multidimensional array with an unknown number of records in the file. The file looks like this: name1 data1 name2 data2 name3 data3 ... nameN dataN How do I load this file into an array for processing, while... (2 Replies)
Discussion started by: leslie02
2 Replies
Login or Register to Ask a Question