0

Looks like I hit a block with something I reckon must be quite simple.

I have a ranking table 1: (file 1)

Ranking  ID

1    A  
2    B  
5    C  
6    D  
12   n

file 2: A
A
A
B
B
B
C
C
n

I need to apply the ranking to another table where IDs are not unique and expect to get this
(desired output):

A   1  
A   1  
A   1  
B   2  
B   2  
B   2  
C   5  
C   5  
n   12 

Can you point me towards a friendly manual ? ;) I would prefer to solve it in command line but Python and Libre office solution can do as well.

  • 2
    What does the second file look like? – Kusalananda Mar 20 '17 at 15:26
  • Based on the information you have thus far shared, awk is probably the tool for this, but without seeing both input files and the desired output, it is difficult to a> be certain, and b> provide a solution. – DopeGhoti Mar 20 '17 at 16:19
  • 1
    There is a join command that could work. – Rob Mar 20 '17 at 16:53
  • Thanks Rob, +1. Dead simple and works straight away, just join file1 file2 > file 3 gives desired output. I wish I could accept this as the answer – xi100f Mar 21 '17 at 9:31
1
$ cat file1
1    A
2    B
5    C
6    D
12   n

$ cat file2
A
A
B
B
B
C
C
n

$ awk 'NR==FNR{Arr[$2]=$1;next}{print $1,Arr[$1]}' file1 file2
A 1
A 1
B 2
B 2
B 2
C 5
C 5
n 12
  • This one works correctly too! Thanks Kamaraj, +1 – xi100f Mar 21 '17 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.