Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two files with tab-seperated values that look like this:


A    1
B    3
C    1
D    4


E    1
B    3
C    2
A    9

I would like to find rows between files 1 and 2 where the string in column 1 is the same, then get the corresponding values. The desired ouput is a single file that looks like this:

B    3    3
C    1    2
A    1    9

Can this be done with a Unix one-liner?

share|improve this question
up vote 9 down vote accepted

GNU coreutils includes the command join that does exactly what you want if line sorting in the result is irrelevant:

join <(sort file1) <(sort file2)

A 1 9
B 3 3
C 1 2

If you want the tabs back, do:

join <(sort file1) <(sort file2) | tr ' ' '\t'

A   1   9
B   3   3
C   1   2

Or use the t option to join.

(<() aka process substitution, requires ksh93 (where the feature originated in), bash or zsh)

share|improve this answer
Now the output is sorted alphabetically, instead of according to file2, but it was not clear whether that would be a problem. – Bernhard Oct 25 '12 at 10:52
ire@localhost: sort -k1 file2 | join file1 -
A 1 9
B 3 3
C 1 2

If you want tabs in the output, then do

sort -k1 file2 | join -t "        " file1 -

where you can construct the tab inside the quotes by pressing CTRL-V <tab>.

share|improve this answer

If you don't want the output sorted but instead have the same order as file2, awk is a good tool to use:

awk '
    NR == FNR {val[$1]=$2; next} 
    $1 in val {print $1, val[$1], $2}
' file1 file2
share|improve this answer

Your Answer


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.