我有一个看起来像这样的文件:
rs1
rs1
rs2
rs11
和文件2一样:
rs1 100
rs2 200
rs11 300
rs21 400
我想获得文件2中文件1的完全匹配(即确切的行数)。我想要的输出:
rs1 100
rs1 100
rs2 200
rs11 300
但我得到了这个:
rs1 100
rs2 200
rs11 300
这是我正在使用的命令:
grep -w -f file1 file2
还试过这个:
grep '^rs\w\+$' file1 | sed 's/^/^/;s/$/\ /' | grep -f - file2
提前谢谢。
答案 0 :(得分:4)
您是否尝试过join
$join file1 file2
rs1 100
rs1 100
rs2 200
rs11 300
来自man join
join - join lines of two files on a common field
For each pair of input lines with identical join fields, write a line
to standard output. The default join field is the first, delimited by
whitespace.
OR
使用awk
$ awk 'FNR==NR{line[$1]=$0; next} ($0 in line){print line[$0]}' file2 file1
rs1 100
rs1 100
rs2 200
rs11 300