Lum – Merging/Combining 2 text files according to numeric field

awkcolumnsjoin;text processing

I have 2 text files

file_1:

14595|Age 35|Salary xx|Position ax|2013|Info 1|Info 2|Info 3|Info 4|Info 5
14649|Age 30|Salary xx|Position az|2015|Info 1|Info 2|Info 3|Info 4|Info 5

file_2:

14595|Address xx|Info 6|Info 7|Info 8
14649|Address xxxz|Info 6|Info 7|Info 8

I want a way to look up file_1 first column (the numeric one) and find its match in file_2 and combine info of both files into file_3 without the numeric key field so the output is like

file_3

Age 35|Salary xx|Position ax|2013|Info 1|Info 2|Info 3|Info 4|Info 5|Address xx|Info 6|Info 7|Info 8
Age 30|Salary xx|Position az|2015|Info 1|Info 2|Info 3|Info 4|Info 5|Address xxxz|Info 6|Info 7|Info 8

Best Answer

Use join:

$ join -t'|' file_1 file_2
14595|Age 35|Salary xx|Position ax|2013|Info 1|Info 2|Info 3|Info 4|Info 5|Address xx|Info 6|Info 7|Info 8
14649|Age 30|Salary xx|Position az|2015|Info 1|Info 2|Info 3|Info 4|Info 5|Address xxxz|Info 6|Info 7|Info 8

-t indicates the field separator.

In order to join works, files must te sorted. You can use sort for it.

Related Question