Using NR==FNR Command

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Using NR==FNR Command
# 1  
Old 07-21-2017
Using NR==FNR Command

Dear All,

I have below two files with me:
file 1:
Code:
A|B
E|F
C|D

file 2:
Code:
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:

Code:
A|B|X|Y
C|D|V|N

Regards,
Ali

Last edited by Corona688; 07-21-2017 at 12:25 PM..
# 2  
Old 07-21-2017
Hello Nebula,

Welcome to forums, hope you will enjoy learning/sharing knowledge/technology experiences here. Kindly use code tags for any Input_file/sample/codes. Could you please try following and let me know if this helps you.
Code:
awk -F"|" 'FNR==NR{val=$1;$1="";a[val]=$0;next} ($1 in a){print $1,$NF a[$1]}' OFS="|"   Input_file2  Input_file1

Thanks,
R. Singh
# 3  
Old 07-21-2017
Hi.

If awk is not a strict requirement, then this seems to be pretty much what the join command
Code:
join '-t|' <( sort $FILE1 ) <( sort $FILE2 )

accomplishes.

Perhaps I missed something.

Best wishes ... cheers, drl
# 4  
Old 07-21-2017
Hey DRL,

Thanks, it worked absolutely fineSmilie

---------- Post updated at 11:51 PM ---------- Previous update was at 11:37 PM ----------

Quote:
Originally Posted by RavinderSingh13
Hello Nebula,

Welcome to forums, hope you will enjoy learning/sharing knowledge/technology experiences here. Kindly use code tags for any Input_file/sample/codes. Could you please try following and let me know if this helps you.
Code:
awk -F"|" 'FNR==NR{val=$1;$1="";a[val]=$0;next} ($1 in a){print $1,$NF a[$1]}' OFS="|"   Input_file2  Input_file1

Thanks,
R. Singh
Thanks RavinderSingh, it worked but gave a bit limited output let me explain what it missed adding one row in file2 in my previous example.

file 1:
Code:
A|B
E|F
C|D

file 2:
Code:
A|X|Y
R|T|I
C|V|N
A|L|K------------- > Updated row

Output with your command returned the same output as described by me earlier:
Code:
A|B|X|Y
C|D|V|N

But the desired output in this case should be like below:
Code:
A|B|X|Y
C|D|V|N
A|B|L|K

Or if files are sorted (like in case of DRL's suggestion)than belowSmilie
Code:
A|B|L|K
A|B|X|Y
C|D|V|N

Please do share the updated command that will help me understanding more about NF and AWK, Thanks once again

Last edited by Nebula; 07-21-2017 at 03:58 PM..
# 5  
Old 07-21-2017
With awk, try:
Code:
awk 'NR==FNR{A[$1]=$0; next} $1 in A{print A[$1], $2, $3}' FS=\| OFS=\| file1 file2

or
Code:
awk 'NR==FNR{A[$1]=$0; next} $1 in A{$1=A[$1]; print}' FS=\| OFS=\| file1 file2

or, less efficient, but shorter:
Code:
awk 'NR==FNR{A[$1]=$0; next} $1=A[$1]' FS=\| OFS=\| file1 file2


Last edited by Scrutinizer; 07-21-2017 at 05:07 PM..
# 6  
Old 07-24-2017
Quote:
Originally Posted by Scrutinizer
With awk, try:
Code:
awk 'NR==FNR{A[$1]=$0; next} $1 in A{print A[$1], $2, $3}' FS=\| OFS=\| file1 file2

or
Code:
awk 'NR==FNR{A[$1]=$0; next} $1 in A{$1=A[$1]; print}' FS=\| OFS=\| file1 file2

or, less efficient, but shorter:
Code:
awk 'NR==FNR{A[$1]=$0; next} $1=A[$1]' FS=\| OFS=\| file1 file2


Thanks dear all worked Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

5. Shell Programming and Scripting

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 1 rs8179466 224176 A ADD 1037 1.066 0.1421 0.8065 1.408 0.4468 ... (3 Replies)
Discussion started by: roofus
3 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 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

9. Shell Programming and Scripting

NR==FNR for getting Col4 value from 2nd file

Multiple versions of this are probably in this section, but could not find one just right for me. My code gives me a zero byte file. Problem: foo 553 403 448 492 451 403 456 560 527 534 529 550 500 447 404 (6 Replies)
Discussion started by: genehunter
6 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