I have two tables with different numbers of columns and rows. I want to find rows using a common column (column B is in common). Here is an example. Could you please help?
file1.txt
A B C D
a b c d
i ii iii iV
* ** # ##
file2.txt
E B
f ff
h b
g gg
k ii
output:
A B C D E
a b c d h
i ii iii iV k
You can do this kind of thing by building a hash / associative array / lookup table e.g. using Awk:
There's also the
join
command - but that requires inputs to be sorted on the common field.To add to the answer by @steeldriver, to do it with
sort
andjoin
:-j 2
tellsjoin
which field is the key.-o
gives the order of the fields in the output, where0
is the common key, and the others areFILENUM.FIELD
. Seeman join
for details.-k 2
tellssort
which field is the key.<( )
isbash
process substitution.Output is: