5

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
2

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.

| improve this answer | |
1

With awk:

$ awk 'BEGIN{FS=OFS="|"}NR==FNR{a[$1]=$0;next}{$1=a[$1]}1' 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
| improve this answer | |

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.