find keyword from file and search in another file

hi dudes;

this is my file.txt:

20101228-180436_Down
a	1
b	2
...
20101228-190436_Rollback
a	1	40
e	3	20
...
20101228-180436_Down
c	2
f	2
c	1
...

and i have a down.txt:

a	1	aa	2	30	bb	1	40
b	2	ab	3	10
c	3	cd	4	50	ac	2	20
c	3	ad	1	0
d	1	af	2	10	av	3	70	ax	4	20
c	2
f	2	bt	5	20
c	1
g	3	ah	4	20	av	1	10	az	5	10
...

i want to search for down portnames in file.txt and get results from down.txt matching first two columns of it. so the result.txt must be:

20101228-180436_Down
a	1	aa	2	30	bb	1	40
b	2	ab	3	10
...
20101228-190436_Rollback
a	1	40
e	3	20
...
20101228-180436_Down
c	2
f	2	bt	5	20
c	1
...

please keep in mind that; a 1 is in both down and rollback list. so i cannot search file.txt line by line. i have to take ONLY down ports and write results next to ONLY down port list.

awk 'NR==FNR{a[$1_$2]=$0;next;}{if(NF==1){if(substr($1,length($1)-3)=="Down") b=1; else b=0; } if(b==1 && a[$1_$2]) $0=a[$1_$2]; print; }' down.txt file.txt
1 Like

anurag;

your result gave the whole down.txt :frowning:

---------- Post updated at 17:54 ---------- Previous update was at 17:51 ----------

oh; no no no; it is ok now :slight_smile: i have just replaced the filenames. it is ok now :slight_smile: thx very muc