Quantcast
Channel: UNIX and Linux Forums
Viewing all articles
Browse latest Browse all 16232

Explanation of FNR in this awk script

$
0
0
To merge mutiple *.tab files as:
file1.tab
Code:

rs1 A A
rs2 A A
rs3 C C
rs4 C C

file2.ind
Code:

rs1 T T
rs2 T T
rs3 G G
rs4 G G

and file3.tab
Code:

rs1 B B
rs2 B B
rs3 L L
rs4 L L

Output:
Code:

file1.tab  file2.tab  file3.tab
AA      TT      BB
AA      TT      BB
CC      GG      LL
CC      GG      LL

which could be done with:
Code:

awk 'BEGIN{for(i=1;i<ARGC;i++)printf ARGV[i]" ";print ""}{a[FNR]=length(a[FNR])?a[FNR]$2$3FS:$2$3FS}END{for(i=0;i++<FNR;)print a[i]}' *.tab
I have hard time to understand a[FNR] and a[FNR]=length(a[FNR]) in the part:
Code:

a[FNR]=length(a[FNR])?a[FNR]$2$3FS:$2$3FS
Can anybody please explain it for me?
Thanks a lot!

Viewing all articles
Browse latest Browse all 16232

Trending Articles