我有一个数据文件file1,如下所示:
sample1
some text
sample1
sample2
some text
sample2
sample3
some text
sample3
...
以及文件2中的参考ID列表:
sample3
sample13
sample21
...
我现在想从file1中提取与file2中的行相对应的信息,因此输出为:
sample3
some text
sample3
sample13
some text
sample13
...
我尝试使用awk和sed,但是不幸的是我无法打印我需要的所有行。
答案 0 :(得分:4)
您很近,但是需要为RS=""
设置file1
(以读取用空行分隔的块而不是行):
$ awk 'NR==FNR{a[$1];next}$1 in a' file2 RS="" file1
sample3
some text
sample3
要分离记录,您可能需要:
$ awk 'BEGIN{ORS="\n\n"}NR==FNR{a[$1];next}$1 in a' file2 RS="" file1
sample3
some text
sample3
samplen
...