help in awk arrays!

Hi, buddies

I am new to shell scripting and trying to solve a problem. I read about arrays in awk that they are quite powerful and are associative in nature.

Awk Gurus Please help!

I have a file:

Id1 pp1 0t4 pp8 xy2
Id43 009y black
Id6 red xy2
Id12 new pp1 black

I have another file :

Id pp1 black new pp8

The output should be like: (Ids of file1 and their matching elements from file2)

Id1 pp1 pp8
Id43 black
Id6 
Id12 new

looking forward for your suggestions.
Cheers!

Thanks a lot! ctsgnb for such an elaborate explaination. Its very informative for me.
But, I was wondering that the last line is printed as it is because it should be

Id12 new

otherwise.

Why would that be? All 3 elements (new pp1 black) are in file2, no?

1 Like

O! Ya! sorry, I even miss wrote in my thread and then just compared it with the same.
Thanks! again.

---------- Post updated at 08:43 PM ---------- Previous update was at 08:34 PM ----------

I have one question. How does it know that the code below is now for second file?

 
{for(k=0;++k<=NF;)

and can we deal with more than two files with awk?

1) This is because of the instruction "next", next skip any further code and reprocess next line from the beginning of awk commands

2) yes, awk can deal with more than 2 files, consider the following code :

$ cat f3
line1
line2
line3
line4
line5
$ nawk '{print NR,FNR,FILENAME,$0}' f3 f1 f2 f3 f2
1 1 f3 line1
2 2 f3 line2
3 3 f3 line3
4 4 f3 line4
5 5 f3 line5
6 1 f1 Id1 pp1 0t4 pp8 xy2
7 2 f1 Id43 009y black
8 3 f1 Id6 red xy2
9 4 f1 Id12 new pp1 black
10 1 f2 Id pp1 black new pp8
11 1 f3 line1
12 2 f3 line2
13 3 f3 line3
14 4 f3 line4
15 5 f3 line5
16 1 f2 Id pp1 black new pp8