NR==FNR confusions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting NR==FNR confusions
# 1  
Old 08-15-2012
NR==FNR confusions

This has been asked and answered hundreds of times, but I can't understand the syntax of awk's NR==FNR trick for merging files and printing the correct columns.

Here's my File 1

Code:
   1   rs8179466     224176    A        ADD     1037      1.066   0.1421   0.8065    1.408       0.4468        0.655
   1   rs3094315     742429    G        ADD     1103     0.8797   0.1553   0.6488    1.193      -0.8254       0.4091
   1   rs3115860     743268    C        ADD     1092     0.9057   0.1663   0.6538    1.255      -0.5958       0.5513
   1   rs3131968     744055    A        ADD       92      1.204   0.6454   0.3398    4.266       0.2876       0.7737
   1   rs3131967     744197    A        ADD     1088     0.8947   0.1705   0.6406     1.25      -0.6524       0.5141

Here's my File 2
Code:
1	rs8179466	0	224176	A	G
1	rs6594028	0	554461	0	G
1	rs10458597	0	554484	0	C
1	rs9701055	0	555296	0	C
1	rs9699599	0	558185	0	A
1	rs3094315	0	742429	G	A
1	rs3115860	0	743268	C	A
1	rs3131968	0	744055	A	G
1	rs3131967	0	744197	A	G
1	rs3115850	0	751010	A	G
1	rs2519016	0	755811	0	T

Final output should look like:

Code:
   1   rs8179466     224176    A        ADD     1037      1.066   0.1421   0.8065    1.408       0.4468        0.655   G
   1   rs3094315     742429    G        ADD     1103     0.8797   0.1553   0.6488    1.193      -0.8254       0.4091   A
   1   rs3115860     743268    C        ADD     1092     0.9057   0.1663   0.6538    1.255      -0.5958       0.5513  A
   1   rs3131968     744055    A        ADD       92      1.204   0.6454   0.3398    4.266       0.2876       0.7737  G
   1   rs3131967     744197    A        ADD     1088     0.8947   0.1705   0.6406     1.25      -0.6524       0.5141  G

In essence, having matched via the second column in File 1, I want to append the fifth column from File 2, as a new 13th column, and output to File 3. Thanks for pointers.


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Using table tags instead makes it really hard to read, thanks.

Last edited by zaxxon; 08-15-2012 at 08:53 AM.. Reason: table tags to code tags
# 2  
Old 08-15-2012
Code:
awk 'NR==FNR{_[$2]=$0; next} _[$2] {print _[$2] "\t" $6}' f1 f2

NR has Number of the Record combined for all files given as input (16 Records). FNR just the Number of Records for the current file.

NR==FNR works like this:
Code:
$ awk '{print "NR:", NR " FNR:", FNR}' f1 f2
NR: 1 FNR: 1             <---- here starts f1
NR: 2 FNR: 2
NR: 3 FNR: 3
NR: 4 FNR: 4
NR: 5 FNR: 5
NR: 6 FNR: 1             <---- here starts f2
NR: 7 FNR: 2
NR: 8 FNR: 3
NR: 9 FNR: 4
NR: 10 FNR: 5
NR: 11 FNR: 6
NR: 12 FNR: 7
NR: 13 FNR: 8
NR: 14 FNR: 9
NR: 15 FNR: 10
NR: 16 FNR: 11

So when NR and FNR are not equal anymore, we must be in the second file f2. The next just helps, that the next following statement is not executed and so we just read in f1 into an associative array for later use, when we are in f2, (ie. when NR==FNR) is not true anymore.

Last edited by zaxxon; 08-15-2012 at 09:27 AM.. Reason: typo
# 3  
Old 08-15-2012
Code:
awk 'NR==FNR{a[$2]=$6;next} {for(x in a){if($2==x){print $0,a[x]}}}' file2 file1

# 4  
Old 08-15-2012
Perfect! Thank you both. Much clearer now.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using NR==FNR Command

Dear All, I have below two files with me: file 1: A|B E|F C|D file 2: A|X|Y R|T|I C|V|N I want to compare 1st column of each file and than print both columns of file 1 and column 2 and 3 of file 2 Sample required output in regards to above files is below: A|B|X|Y C|D|V|N (5 Replies)
Discussion started by: Nebula
5 Replies

2. Shell Programming and Scripting

Explanation of FNR in this awk script

To merge mutiple *.tab files as: file1.tab rs1 A A rs2 A A rs3 C C rs4 C Cfile2.ind rs1 T T rs2 T T rs3 G G rs4 G Gand file3.tab rs1 B B rs2 B B rs3 L L rs4 L LOutput: file1.tab file2.tab file3.tab AA TT BB AA TT BB CC GG LL CC GG ... (4 Replies)
Discussion started by: yifangt
4 Replies

3. 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

4. Shell Programming and Scripting

Help with Alternative for NR==FNR

Hi, I have an issue with the below script nawk 'NR==FNR{a=$4" "$5}NR>FNR{print NF?$0:a"\n";if(/^cn:/) x=$0}' FS="" in1.txt in2.txt > out1.txt It is taking too long to get a string from in1.text, search for the string in in2.txt and create a new file out1.txt. Is there any alternative way we... (1 Reply)
Discussion started by: Samingla
1 Replies

5. Shell Programming and Scripting

How to use NA and FNR?

Hi i have file1: conn=232257 client=16218.19488.218.86:51237 protocol=LDAP file2: conn=232257 dn="uid=apple,ou=xxxx,ou=usfgfhfers,dc=example,dc=com" conn=232370 dn="uid=ball,ou=yyyyyy,ou=usfhfhfhers,dc=example,dc=com" In the output file it should match first column from above both files... (2 Replies)
Discussion started by: buzzme
2 Replies

6. 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

7. Shell Programming and Scripting

NR=FNR

cat file1 1 a b c 2 d e f 3 a f r cat file2 a c e output should be 1 3 means: if field 1 of file2 matches filed 2 of file1 then print field 1 of file1 I know that it can be done using awk NR=FNR. But not able to acheive it. Thanks in advance. (9 Replies)
Discussion started by: shaan4uster
9 Replies

8. UNIX and Linux Applications

Problmes/Confusions with WINE!

I want to use WINE. I have some confusions with it. Does WINE support only access to Windows apps which are installed on Windows in case if it is Dual boot? Can I install standalone Windows application directly on Linux if its not Dual boot? Please explain. (2 Replies)
Discussion started by: nixhead
2 Replies

9. Solaris

swap space confusions

hi, i am a little bit confused over swap space issues on solaris. what i know is that mostly all solaris swap space is configured on /tmp as a tmpfs. but i do receive alerts from my monitoring server that one of my server has exceeded 95% treshold. when checked at tmp, it shows only 12%. ... (1 Reply)
Discussion started by: cromohawk
1 Replies

10. 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
Login or Register to Ask a Question