Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

For an example, I have 2 files having following info:

File #1:

12
13
14
15

File #2:

12 1
13 2
14 2
15 6
16 7
17 8

Output File:

1
2
2
6

In the output file, I want only the second column values of file #2 which has matched with the first column of file #1. Is there any utility function for that in Linux? I'm new in shell scripting, can anyone help me out?

share|improve this question
add comment

1 Answer

you could use the join command to join the files on a specific column, and awk to parse the output.

To join these files on column 1 pass the parameters -j 1 to the join command:

usr@srv % join -j 1 test test2 
12 1

13 2

14 2

15 6

Afterwards use awk to print only the second column:

usr@srv % join -j 1 test test2 | awk '{print $2}'
1

2

2

6
share|improve this answer
5  
No need for awk: join -o 2.2 test test2 –  Stephane Chazelas Sep 21 '12 at 8:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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