我有两个文件;我想加入他们。
$cat t1
1 1.2
2 2.2
$cat t2
1
2
1
我想要下面的输出
$cat joind.txt
1 1.2
2 2.2
1 1.2
但是当我使用join
命令,第三行不会出现在输出中。
一个简单的awk
就足够了:
awk 'FNR==NR{a[$1]=$2;next} {print $1, a[$1]}' t1 t2
1 1.2
2 2.2
1 1.2
Breakup:
NR == FNR { # While processing the first file
a[$1] = $2 # store the second field by the first
next # move to next record in 1st file
}
{ # while processing the second file
print $1, a[$1] # print $1 and the remembered
# value from the first file.
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)