search 3 file and write to 4th file (a bit complex)

hi buddies;

rollbackip.txt:

10.14.3.65	2
10.14.3.65	3
...

lookup.txt:

...
10.14.3.65	2	10.14.5.55	1	55	10.14.6.66	1	66
10.14.3.65	3	10.14.7.77	3	77	10.14.8.88	2	88	10.14.9.99	4	99
...

ip-port.txt

...
port111	3	10.14.5.55	57
port111	2	10.14.5.55	51
port111	1	10.14.5.55	59 -> this line must be taken
...
port111	1	10.14.6.66	63 -> this line must be taken
port111	2	10.14.6.66	64
port111	3	10.14.6.66	65
port111	4	10.14.6.66	66
...
port113	3	10.14.7.77	72 -> this line must be taken
port113	1	10.14.7.77	74
...
port140	1	10.14.8.88	84
port140	2	10.14.8.88	84 -> this line must be taken
port140	5	10.14.8.88	84
...
port613	4	10.14.9.99	92 -> this line must be taken
port613	2	10.14.9.99	92
...

i need a code for;

  • take ip & sector numbers, from rollbackip.txt
  • search ip and sector numbers related to each, from lookup.txt
  • get original values of each ip & sector, from ip-port.txt and write these original values to file result.txt.

to conclude, the new rollbackip.txt file should be:
result.txt:

10.14.3.65	2	10.14.5.55	1	59	10.14.6.66	1	63
10.14.3.65	3	10.14.7.77	3	72	10.14.8.88	2	84	10.14.9.99	4	92
...

hope to tell it clearly :slight_smile:

thx

 
awk '{if(FILENAME=="rollbackip.txt"){a[$1$2]++;next;}if(FILENAME=="ip-port.txt"){b[$3$2]=$4;next;}if(FILENAME=="lookup.txt"){if(a[$1$2]){for(i=3;i<=NF-2;i=i+3) if(b[$i$(i+1)]) $(i+2)=b[$i$(i+1)];print;}}}' rollbackip.txt ip-port.txt lookup.txt

:frowning: error:

nawk: syntax error at source line 1
context is
   >>> {if(FILENAME=="rollbackip.txt"){a[$1$2]++;next;}if(FILENAME=="ip-port.txt"){b[$3$2]=$4;next;}if(FILENAME=="lookup.txt"){if( <<<
nawk: illegal statement at source line 1
nawk: syntax error at source line 1

do i have to change {if(FILENAME==/home/gcsw/.../rollbackip.txt like this? i have tried both, but no result..

PLease be careful with [$1$2] and [$3$2] constructs, for data like

192.168.1.1    21
192.168.1.12    1

will munge together. Awk "supports" multidimensional arrays -- it interpolates the value of SUBSEP.

Below, here is my proposal:

BEGIN {
    rollbackfile = ARGV[1];
    lookupfile   = ARGV[2];
    ipportfile   = ARGV[3];
}

/^#/ { next; }

FILENAME == rollbackfile {
    rollback[$1, $2]++;
    next;
}

FILENAME == lookupfile  {
    if (rollback[$1, $2]) {
        for (i = 3; i < NF; i += 3) { lookup[$(i+1), $(i)]++; }
    }
    next;
}

FILENAME == ipportfile {
    if (lookup[$2, $3]) { print; }
    next;
}

{
    print FILENAME "(" FNR "): unparsed line -", $0 > "/dev/stderr";
}

The files are in order on the command line.

1 Like

Try this,

 awk 'NR==FNR{for (i=3;i<=NF;i++){y=i;i++;b[$y$i]=$1" "$2;++i};next} {if(FILENAME=="rollbackip.txt"){f[$1" "$2]++;next}}b[$3$2]{w[$3" "$2" "$4]=b[$3$2]} END {for (k in f){printf RS k FS;for (l in w) {if(k==w[l]){printf FS l FS}}}}' lookup.txt ip-port.txt rollbackip.txt
awk 'NR==FNR{A[$3,$2]=$4;next} NF>2{for(i=3;i<=NF;i+=3)$(i+2)=A[$i,$(i+1)];B[$1,$2]=$0;next} B[$1,$2]{print B[$1,$2]}' OFS='\t' ip-port.txt lookup.txt rollbackip.txt
2 Likes

That's the best.:b:

1 Like

Scrutinizer;

thanksss very much. your solutions are always perfecto :slight_smile: i'm using it :slight_smile: