在单个字段中在单个字段中搜索,该字段包含两个文件,其中包含两个文件,使用awk?
我有两个文件。
file01.tab
:
Q86IC9 PGEN_.00g000010
P04177 PGEN_.00g000020
Q8L840 PGEN_.00g000050
Q61043 PGEN_.00g000060
A1E2V0 PGEN_.00g000080
P34456 PGEN_.00g000090
P34457 PGEN_.00g000120
O00463 PGEN_.00g000210
Q00945 PGEN_.00g000230
Q5SWK7 PGEN_.00g000240
file> file02.tab
:
Q86IC9;Q552T5 omt5
P04177 Th
Q8L840;O04092;Q9FT71 RECQL4A
Q61043;A0A1Y7VJL5;B2RQ73;B7ZMZ9;E9Q488;E9Q4S3;Q674R4;Q6ZPM7 Nin
A1E2V0 BIRC3
P34456 Uncharacterized
P34457 uncharacterized
O00463;B4DIS9;B4E0A2;Q6FHY1 TRAF5
Q00945 RING
Q5SWK7;Q8BXX5;Q9CXG1 Rnf145
我想在file> file01.tab
中使用第一列与中的第一列加入file02.tab
。我可以使用grep
进行此操作,但是我需要以以下方式对输出进行格式:
PGEN_.00g000010 Q86IC9;Q552T5 omt5
PGEN_.00g000020 P04177 Th
QPGEN_.00g000050 Q8L840;O04092;Q9FT71 RECQL4A
非常接近成功
awk 'NR==FNR{a[$1]=$0; next} ($1 in a){print $2,a[$1]}' file02.tab file01.tab
我使用以下awk
代码 -liner将产生以下内容:
PGEN_.00g000020 P04177 Th
PGEN_.00g000080 A1E2V0 BIRC3
PGEN_.00g000090 P34456 Uncharacterized
PGEN_.00g000120 P34457 uncharacterized
PGEN_.00g000230 Q00945 RING
PGEN_.00g000280 Q8ZXT3 protein
PGEN_.00g000300 Q5REG4 DTX3
PGEN_.00g000450 A0JMR6 mysm1
PGEN_.00g000490 Q7D513 Hercynine
PGEN_.00g000530 A6H769 RPS7
代码dik 不是在file> file02.tab
$ 1
中查找匹配项,其中有一个内场半柱分隔线。它只会在$ 1
中找到具有单个条目的匹配项。
显然,grep
可以使用两个输入文件处理搜索,但是我不知道如何从grep
结果中格式化输出,因为格式需要从两个输入中进行信息文件。
有什么方法可以用awk
单线线完成此操作,或者我应该将小脚本放在一起?
I have two files.
file01.tab
:
Q86IC9 PGEN_.00g000010
P04177 PGEN_.00g000020
Q8L840 PGEN_.00g000050
Q61043 PGEN_.00g000060
A1E2V0 PGEN_.00g000080
P34456 PGEN_.00g000090
P34457 PGEN_.00g000120
O00463 PGEN_.00g000210
Q00945 PGEN_.00g000230
Q5SWK7 PGEN_.00g000240
file02.tab
:
Q86IC9;Q552T5 omt5
P04177 Th
Q8L840;O04092;Q9FT71 RECQL4A
Q61043;A0A1Y7VJL5;B2RQ73;B7ZMZ9;E9Q488;E9Q4S3;Q674R4;Q6ZPM7 Nin
A1E2V0 BIRC3
P34456 Uncharacterized
P34457 uncharacterized
O00463;B4DIS9;B4E0A2;Q6FHY1 TRAF5
Q00945 RING
Q5SWK7;Q8BXX5;Q9CXG1 Rnf145
I want to use the first column in file01.tab
to join with the first column in file02.tab
. I could do this with grep
, but I need the output to be formatted in the following fashion:
PGEN_.00g000010 Q86IC9;Q552T5 omt5
PGEN_.00g000020 P04177 Th
QPGEN_.00g000050 Q8L840;O04092;Q9FT71 RECQL4A
I've come very close to success using the following awk
code:
awk 'NR==FNR{a[$1]=$0; next} ($1 in a){print $2,a[$1]}' file02.tab file01.tab
That one-liner will produce the following:
PGEN_.00g000020 P04177 Th
PGEN_.00g000080 A1E2V0 BIRC3
PGEN_.00g000090 P34456 Uncharacterized
PGEN_.00g000120 P34457 uncharacterized
PGEN_.00g000230 Q00945 RING
PGEN_.00g000280 Q8ZXT3 protein
PGEN_.00g000300 Q5REG4 DTX3
PGEN_.00g000450 A0JMR6 mysm1
PGEN_.00g000490 Q7D513 Hercynine
PGEN_.00g000530 A6H769 RPS7
The code does not find matches in file02.tab
$1
where there is an in-field semi-colon delimiter. It will only find matches that have a single entry in $1
.
Obviously, grep
can handle the searching using two input files, but I don't know how to format the output from the grep
results, since the formatting requires info from both input files.
Is there any way to accomplish this with an awk
one-liner or should I put together a small script instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您能否尝试以下内容:
输出:
fs ='[; [:Space:]]+'
将线路分配在一系列分号或空间上人物。
Would you please try the following:
Output:
FS='[;[:space:]]+'
splits the line on a sequence of semicolons or spacecharacters.
您可以尝试
你得到,
you can try with split in
awk
you get,
如果这仅是关于匹配半符号划界字符串中的第一个值,则还可以使用split并比较第一个值:
或删除所有从第一个分号开始:
输出
If this is only about matching the first value in the semicolon delimited string, you can also use split and compare the first value:
Or removing all starting at the first semicolon:
Output