[英]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.