I have two files; I want to join them.
$cat t1
1 1.2
2 2.2
$cat t2
1
2
1
I want to have the output below
$cat joind.txt
1 1.2
2 2.2
1 1.2
but when I use the join
command, the third line does not appear in the output.
I have two files; I want to join them.
$cat t1
1 1.2
2 2.2
$cat t2
1
2
1
I want to have the output below
$cat joind.txt
1 1.2
2 2.2
1 1.2
but when I use the join
command, the third line does not appear in the output.
join
requires that both files to be sorted. If you sort them first, you'll get all your outputWithout the sort:
This assumes that the order of your inputs don't need to be preserved; if they do, you will need a custom script like other answers have provided.
you can try
AWK
:awk 'NR==FNR{a[$1]=$2}NR>FNR{print $1,a[$1]}' t1 t2
Something like the following with do:
If I understand you want to match the first column of
t1
with the values int2
. Sot1
is a dictionnary andt2
the wanted keys.If so, you can use this:
How does it work?
xargs
will execute the commandgrep
for each one entry-n1
oft2
. The-I{}
allows me to put the value where I want to.Then I execute
grep
which match the wanted value from the dictionary using a regular expression.Alternatively you can play with Perl :)
A simple
awk
is suffice for this:Breakup: