繁体   English   中英

使用grep -f从另一个文件获取确切的行数

[英]Use grep -f to get the exact number of lines from another file

我有一个文件1,看起来像这样:

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

先感谢您。

您是否尝试过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.

要么

使用awk

$ awk 'FNR==NR{line[$1]=$0; next} ($0 in line){print line[$0]}' file2 file1
rs1 100
rs1 100
rs2 200
rs11 300

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM