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.

I have two files in file A, there are sequence_numbers in the other file B, there are many columns, and the first column is sequnce numbers, I want to get a files with all the lines in the B with the sequence numbers which are in the A how can I achieve this? thanks

like file A

1
3
8
9
20

file B

1 kfjk 3243424
2 fkdkf 23543592
3 iefjk 21493402
7 dlafdl 23435231
8 kfkdlkf 309834
share|improve this question
2  
Better provide sample input & output. –  sputnick Nov 16 '12 at 11:42
add comment

1 Answer

up vote 2 down vote accepted

You want join (1), I guess:

For each pair of input lines with identical join fields, write a line to standard output. The default join field is the first, delimited by whitespace. When FILE1 or FILE2 (not both) is -, read standard input.

[0 1075 12:50:10] ~/temp/sx % join A B
1 kfjk 3243424
3 iefjk 21493402
8 kfkdlkf 309834
join: file 1 is not in sorted order

OK, so apparently you need to combine this with sort (1) to sort by alpha value (not numerical value, so 20 < 3)

join <(sort A) <(sort B) works for me, but that looks weird and might be a zsh extension. There's no harm in doing

sort A > A.tmp; sort B > B.tmp; join A.tmp B.tmp

(As usual, check the man pages for pitfalls.)

share|improve this answer
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.