[英]Use grep -f to get the exact number of lines from another file
I have a file 1 that looks like this: 我有一个文件1,看起来像这样:
rs1
rs1
rs2
rs11
And file 2 like this: 并像这样归档2:
rs1 100
rs2 200
rs11 300
rs21 400
I want to get the exact match of file 1 in file 2 (that is the exact number of lines). 我想获得文件2中文件1的完全匹配(即行的确切数量)。 My desired output:
我想要的输出:
rs1 100
rs1 100
rs2 200
rs11 300
But I am getting this: 但是我得到这个:
rs1 100
rs2 200
rs11 300
This is the command I am using: 这是我正在使用的命令:
grep -w -f file1 file2
Also tried this: 还尝试了这个:
grep '^rs\w\+$' file1 | sed 's/^/^/;s/$/\ /' | grep -f - file2
Thank you in advance. 先感谢您。
Have you tried join
您是否尝试过
join
$join file1 file2
rs1 100
rs1 100
rs2 200
rs11 300
From man join
从
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 要么
using awk
使用
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.