Lum – Join two files with different fields number

columnsjoin;text processing

I have two files:
s2.txt

14 3KC12828ACBA 
43 8DG59242BAAD 
25 8DG60566AAAF 
6 8DG60912AAAF

and pbas.txt:

3AG33662AAAC
3KC12828ACBA
8DG59242BAAD
8DG60349AAAC
8DG60565AAAG
8DG60566AAAF
8DG60568AAAC
8DG60912AAAF
8DG62635AAAC

Using bash on UNIX I want to join files in order to obtain one file like that:

3AG33662AAAC
3KC12828ACBA 14
8DG59242BAAD 43
8DG60349AAAC
8DG60565AAAG
8DG60566AAAF 25
8DG60568AAAC
8DG60912AAAF 6
8DG62635AAAC

How can make that?

Best Answer

That's what join does:

join -2 2 -a 1 pbas.txt s2.txt

The options say:

  • -2 2: the second file uses the second column to store the key
  • -a 1: output all lines from file 1, even if there's no match in file 2.
Related Question