Visit Our UNIX and Linux User Community

Explanation of FNR in this awk script

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Explanation of FNR in this awk script
# 1  
Old 05-20-2015
Explanation of FNR in this awk script

To merge mutiple *.tab files as:
rs1 A A
rs2 A A
rs3 C C
rs4 C C

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

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

AA       TT       BB
AA       TT       BB
CC       GG       LL
CC       GG       LL

which could be done with:
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:

Can anybody please explain it for me?
Thanks a lot!

Last edited by yifangt; 05-20-2015 at 09:00 PM..
# 2  
Old 05-20-2015
That expression is a mouthful; the ? is a short-form if/else/equals which allows for brief but sometimes impenetrable expressions. To spell it out:

a[FNR]=length(a[FNR])? # if length(a[FNR]) != 0
        a[FNR]$2$3FS # a[FNR] = a[FNR] $2 $3 FS
        :                   # else
        $2$3FS          # a[FNR]=$2 $3 FS

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-20-2015
I think I get the ? part, but not the a[FNR]. When I tried {print a[FNR], length(a[FNR])}, all are "0". It seems to me a[FNR] is dynamically accumulating one after another file, so that files are appending at the end one by one.
I tried paste *.tab, but here I am trying to understand a[FNR]. More clue please!
# 4  
Old 05-20-2015
In awk, NR is the number of records read so far from all of the input files and FNR is the number of records read so far from the current input file.

So, when you read from the 1st input file a[FNR] will be the default uninitialized empty string and length(a[FNR]) will be zero which will then cause a[FNR] to be set to the contents of field 2 from that line, field 3 from that line, and the field separator ($2$3FS). And, when the awk reads the corresponding line from subsequent input files, length(a[FNR]) will not be zero and a[FNR] will be set to the current a[FNR] with field 2 from the current line in the new file, field 3 from the current line in the new file, and FS appended to the end of it (a[FNR]$2$3FS).

Is this any clearer for you?
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 05-21-2015
Thank Don and Corona!
Realized there was a precedence in the script, and I get it now. I rewrote that part:
awk '{a[FNR]=(length(a[FNR])?a[FNR]$2$3FS:$2$3FS); print FNR, a[FNR], length(a[FNR])}
END{print "-------------"; for (i =0; i++<FNR;) print i, a[i], length(a[i])}' *.tab

and the output is:
1 AA  3
2 AA  3
3 CC  3
4 CC  3
1 AA TT  6
2 AA TT  6
3 CC GG  6
4 CC GG  6
1 AA TT BB  9
2 AA TT BB  9
3 CC GG LL  9
4 CC GG LL  9
1 AA TT BB  9
2 AA TT BB  9
3 CC GG LL  9
4 CC GG LL  9

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Assigning a variable to be the value of FNR at a certain line

Sorry for the probably strangely worded title but I don't really know how else to put it. Background context: Post processing LAMMPS simulation data. tl;dr: I'm making two spheres collide, every defined timestep the simulation outputs a bunch of data including total energy of the particles,... (10 Replies)
Discussion started by: ThomasP
10 Replies

2. Shell Programming and Scripting

awk --> selective printout with FNR

Hi everybody! need some awk-support. i want a line-selective printout of a file. wat i normally will do with ... awk ' FNR==8' sample.txt But now i need the data from line 8, 10 and the following data from line13 to 250 wich is not end of the file. I tried allready to combine it but without... (2 Replies)
Discussion started by: IMPe
2 Replies

3. Shell Programming and Scripting

Tip: alternative for NR==FNR in awk

Example: $ cat file1 2 3$ cat file2 1 2 3 4 5 6The following awk script works like a charm, NR==FNR is true for file1, the remainder runs for file2: awk ' NR==FNR {A; next} ($1 in A) ' file1 file2 2 3Now have an empty file1: >file1and run the awk script again. The result is empty... (8 Replies)
Discussion started by: MadeInGermany
8 Replies

4. Shell Programming and Scripting

Awk FNR==NR question

awk -F'' 'FNR==NR {a=$2; next} {$1=a} 1' $useralias ${entries} >> ${entries}_2 Hi, Is there anyway to alter this command so that if it does not find a match it will just leave the line alone instead of replacing what it doesn't find with a blank space? (4 Replies)
Discussion started by: Jazmania
4 Replies

5. UNIX for Dummies Questions & Answers

awk NR==FNR output control

Hi Guys, I have two files: f1: A B C D E F G H f2: A X Y Z f1 has 48000 lines, and f2 has 68. I have been matching f1 $3 to f2 $1, and getting f3: A A B C D E F G I would like f3 too look like this: A X Y Z A B C D E F G (2 Replies)
Discussion started by: heecha
2 Replies

6. UNIX for Dummies Questions & Answers

Multiple Column print after lookup using NR==FNR (awk)

foo.txt FAMID IID AFF SEX Group AgeCat Dis1 Dis2 Dis3 Dis4 Dis5 Dis6 Dis6 AMD0001 Mayo_49542 1 2 AMD 8 1 1 1 1 1 1 1 AMD0002 Mayo_49606 1 1 AMD 3 1 1 1 1 ... (7 Replies)
Discussion started by: genehunter
7 Replies

7. Shell Programming and Scripting

error "awk: (FILENAME=- FNR=23) fatal: division by zero attempted"

Hi , I have file : after i run this command : there are error can we print blank line if output error ?? thanks.. ^^ (4 Replies)
Discussion started by: justbow
4 Replies

8. Shell Programming and Scripting

awk NR==FNR compare 2 files produce a 3rd

hi, i have two files, both with 3 columns, the 3rd column has common values between the two files and i want to produce a 3rd file with 4 columns. file 1 a, ,b c file 2 a, b ,d I want to compare the 3rd value and if a match print to file 3 with the 3 columns from the first file... (11 Replies)
Discussion started by: borderblaster
11 Replies

9. Shell Programming and Scripting

Awk: different between NR and FNR

As I know: FNR: The ordinal number of the current record in the current file. NR: The ordinal number of the current record from the start of input. I don't understand really differency between NR and FNR. Who can explain it for me? And give me an example. Thanks (1 Reply)
Discussion started by: anhtt
1 Replies

10. Shell Programming and Scripting

AWK explanation

Hi, Could anyone please explain why we have arr=1 - what does this statement do? awk -F\; 'FNR==NR{arr=1;next};$3 in arr' core.txt gmrd.txt Any help appreciated (2 Replies)
Discussion started by: penfold
2 Replies

Featured Tech Videos