从与另一个文件中的行对应的一个文件中提取数据

问题描述 投票:0回答:1

我有一个数据文件file1如下:

sample1
some text
sample1

sample2
some text
sample2

sample3
some text
sample3
...

和file2中的引用ID列表:

sample3
sample13
sample21
...

我现在想要从file1中提取与file2中的行相对应的信息,因此输出将是:

sample3
some text
sample3

sample13
some text
sample13

...

我试图使用awk和sed,但不幸的是我无法打印出我需要的所有行。

linux unix awk sed grep
1个回答
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
...
© www.soinside.com 2019 - 2024. All rights reserved.