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 2 files, one having 2 columns, another having 1 column. The second file is sorted using sort -u. Now the task is I need to join this column with the first column of the first file, which is not sorted, so what will be the syntax? will this work? join -j 1 file2.txt sort -s -n -k 1 file1.txt? The output I want is actually the 2nd column of file 2 after joining and the uniq entries in it.

File 2


File 1

2  500
1  5000 
1  300
3  3000 
3  300
4  450


share|improve this question
"Will this work?" Please try it out and check for yourself. I don't understand how your output could have four lines when file 1 only has three. – Mat Sep 28 '12 at 8:30
Do you need to use File 2? Is sorting File 1 and then removing unwanted entries, e.g., lines with prefix >= 4 || duplicate values out of the question? – tojrobinson Sep 28 '12 at 8:30
Hello Mat, think like this: The second file is joined against the file 1 on column 1 and then the uniq entries are taken from column 2 of file 1. I tried but I think syntactically its not correct, thats what I was asking if its ok or not. – MiNdFrEaK Sep 28 '12 at 8:34
up vote 1 down vote accepted

No need to use non-standard process substitution (<(...)) here:

sort file1 | join -o1.2 - file2 | uniq
share|improve this answer
This only works if file2 is already sorted. To really avoid the bashisms, the sorted files would need to be written to a temporary file. – jordanm Sep 28 '12 at 15:10
@jordanm. file2 is sorted according to the OP's question. process substitution is a ksh feature originally, not a bashism. You don't need a temp file, you can use named or /dev/fd/n if supported as ksh/bash/zsh internally do for <(...). – Stéphane Chazelas Sep 28 '12 at 17:10
join file2.txt <(sort file1.txt) | awk '{print $2}'
share|improve this answer

One way using sort + awk. I sort the other file by its first number and in stable mode. In awk I compare what keys from file1 match with the keys of file2 and are not repeated, printing them:

sort -snk1,1 file1 | awk '
    FNR == NR { 
        keys[ $1 ] = 1; 
    !values[ $2 ] && keys[ $1 ] { 
        printf "%s\n", $2; 
        values[ $2 ] = 1; 
' 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.