Merging Columns

Hi,

Can you please help me.

I have 2 files to merge

File1

1251 743
1250 742
1249 741
1248 749
1247 722
1246 740
1245 739
1244 740
1243 705
1242 631
1241 590

File2

1250 247
1249 241
1248 244
1247 300
1245 276
1244 292
1243 272
1242 236
1241 263

I have try

awk NR==FNR{a[$1]=$2;next}$1 in a{print $0,a[$1]} file1 file2

I get this

1250 247 742
1249 241 741
1248 244 749
1247 300 722
1245 276 739
1244 292 740
1243 272 705
1242 236 631
1241 263 590

But I will like something like this.

1251 743    
1250 742 247
1249 741 241
1248 749 244
1247 722 300
1246 740    
1245 739 276
1244 740 292
1243 705 272
1242 631 236
1241 590 263

How I can get my desired output..

Thanks :b:

Just flip the two files and remove the condition:

awk 'NR==FNR{a[$1]=$2;next} {print $0,a[$1]}' file2 file1
1 Like

Hello jiam912,

If order doesn't matter for you, following may help too.

awk 'FNR==NR{A[$1]=$2;next} ($1 in A){print $0 OFS A[$1];delete A[$1]} END{for(i in A){print i OFS A}}'  file1 file2

Output will be as follows.

1250 247 742
1249 241 741
1248 244 749
1247 300 722
1245 276 739
1244 292 740
1243 272 705
1242 236 631
1241 263 590
1251 743
1246 740

Thanks,
R. Singh

1 Like