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

Regards,
Ali

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.

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

Hi.

If awk is not a strict requirement, then this seems to be pretty much what the join command

join '-t|' <( sort $FILE1 ) <( sort $FILE2 )

accomplishes.

Perhaps I missed something.

Best wishes ... cheers, drl

Hey DRL,

Thanks, it worked absolutely fine:)

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

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:

A|B
E|F
C|D

file 2:

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:

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

But the desired output in this case should be like below:

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 below:)

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

With awk, try:

awk 'NR==FNR{A[$1]=$0; next} $1 in A{print A[$1], $2, $3}' FS=\| OFS=\| file1 file2

or

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

or, less efficient, but shorter:

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

Thanks dear all worked :slight_smile: