Not as elegant as the POSIX FILENAME solution, but handy for dusty, old awks that lack too many features. You can make a compound statement that manipulates your data before sending it to awk in a couple of ways...
Option 1
First, you could output the filenumber on its own before each file that you send to awk. So, if your files look like this:
file1
Line 1 of 1
file2
Line 1 of 2
Line 2 of 2
file3
Line 1 of 3
Line 2 of 3
Line 3 of 3
You could do this:
{ echo 1; cat file1; echo 2; cat file2; echo 3; cat file3; }
1
Line 1 of 1
2
Line 1 of 2
Line 2 of 2
3
Line 1 of 3
Line 2 of 3
Line 3 of 3
and pipe that into awk and then pick up the filenumber every time the number of fields is 1
{ echo 1; cat file1; echo 2; cat file2; echo 3; cat file3; } | awk 'NF==1{file=$1;next} {print file,$0}'
1 Line 1 of 1
2 Line 1 of 2
2 Line 2 of 2
3 Line 1 of 3
3 Line 2 of 3
3 Line 3 of 3
Option 2
Or, you could edit the filenumber onto the start, or end, of every line so it is available as $1 inside awk, like this:
{ sed 's/^/1 /' file1; sed 's/^/2 /' file2; sed 's/^/3 /' file3; }
1 Line 1 of 1
2 Line 1 of 2
2 Line 2 of 2
3 Line 1 of 3
3 Line 2 of 3
3 Line 3 of 3
So, now you can do
{ sed 's/^/1 /' file1; sed 's/^/2 /' file2; sed 's/^/3 /' file3; } | awk '{file=$1; ...}'
I'm still voting for @fedorqui's solution though :-)
FNR== "The input record number in the current input file."NR== "The total number of input records seen so far." soFNR==NRfor the first file and is different for every other file. What are you trying to do with your third file?