0
votes

I could write it all with if statements once I determine the number of fields, but I believe the power of AWK can simplify this. I hope the logic is clear within the if statements and the output below.

Determine the link number from the first node ($2) to the second node (3) in file1

if($2 && $3 (file1) == $1 and $3 (file2));
    print $2 of file2 in between $2 and $3 of file1
elif($2 && $3 (file1) == $3 && $1 (file2));
    print $4 of file2 in between $2 and $3 of file1
else
    print no_int in between $2 and $3 of file1

If applicable, determine the link number from the second node ($3) to the third node (4) in file1

if($3 && $4 (file1) == $1 and $3 (file2));
    print $2 of file2 in between $2 and $3 of file1
elif($3 && $4 (file1) == $3 && $1 (file2));
    print $4 of file2 in between $3 and $4 of file1
else
    print no_int in between $3 and $4 of file1

If applicable, determine the link number from the third node ($4) to the forth node (5) in file1

if($4 && $5 (file1) == $1 and $3 (file2));
    print $2 of file2 in between $4 and $5 of file1
elif($4 && $5 (file1) == $3 && $1 (file2));
    print $4 of file2 in between $4 and $5 of file1
else
    print no_int in between $4 and $5 of file1  

...So on and so forth...

File 1: Path Table

N1-N4   NODE3   NODE4
N1-N5   NODE2   NODE4   NODE5
N1-N6   NODE3   NODE4   NODE5   NODE6

File 2: Link Table

NODE1   1   NODE2   2
NODE1   3   NODE3   4
NODE2   5   NODE4   6
NODE4   8   NODE3   7
NODE4   9   NODE5   10
NODE5   11  NODE6   12

Output:

N1-N4   NODE3   7   NODE4
N1-N5   NODE2   5   NODE4   9   NODE5   
N1-N6   NODE3   7   NODE4   9   NODE5   11 NODE6    

Network Map

NODE1--1--2--NODE2
  |            |
  3            5
  |            |
  4            6
  |            |
NODE3--7--8--NODE4--9--10--NODE5--11--12--NODE6

EDIT Matching Logic: Take for example a line from file1:

N1-N4   NODE3   NODE4

This should match this line from file two:

NODE4   8   NODE3   7

$2 && $3 (file1) does not match $1,$3 (file2) - NODE3 NODE4 != NODE4 NODE3. But $2,$3 does equal $3,$1 - NODE3 NODE4 == NODE3 NODE4. So print $4 from file2 - "NODE3 7 NODE4". If $2 && $3 (file1) DID match $1,$3 (file2), print $2 from file2 - "NODE3 8 NODE4"

1

1 Answers

1
votes

something like this should do, it's important to set the sample input/output to include the edge cases you're interested in.

$ awk 'NR==FNR{a[$1,$3]=$2; a[$3,$1]=$4; next} 
              {for(i=2;i<NF;i++) 
                 {k=a[$i,$(i+1)]; 
                  $i=$i OFS (k?k:"NaN")}}1' link path


N1-N4 NODE3 7 NODE4
N1-N5 NODE2 5 NODE4 9 NODE5
N1-N6 NODE3 7 NODE4 9 NODE5 11 NODE6