在单个字段中在单个字段中搜索,该字段包含两个文件,其中包含两个文件,使用awk?

发布于 2025-01-21 23:08:06 字数 2213 浏览 1 评论 0原文

我有两个文件。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阳光下慵懒的猫 2025-01-28 23:08:06

您能否尝试以下内容:

awk -v FS='[;[:space:]]+' 'NR==FNR {a[$1]=$2; next} $1 in a {print a[$1], $0}' file01.tab file02.tab

输出:

PGEN_.00g000010 Q86IC9;Q552T5                                                omt5
PGEN_.00g000020 P04177                                                       Th
PGEN_.00g000050 Q8L840;O04092;Q9FT71                                         RECQL4A
PGEN_.00g000060 Q61043;A0A1Y7VJL5;B2RQ73;B7ZMZ9;E9Q488;E9Q4S3;Q674R4;Q6ZPM7  Nin
PGEN_.00g000080 A1E2V0                                                       BIRC3
PGEN_.00g000090 P34456                                                       Uncharacterized
PGEN_.00g000120 P34457                                                       uncharacterized
PGEN_.00g000210 O00463;B4DIS9;B4E0A2;Q6FHY1                                  TRAF5
PGEN_.00g000230 Q00945                                                       RING
PGEN_.00g000240 Q5SWK7;Q8BXX5;Q9CXG1                                         Rnf145
  • fs ='[; [:Space:]]+'将线路分配在一系列分号或空间上
    人物。
  • 为简单起见,我已经切换了文件顺序。

Would you please try the following:

awk -v FS='[;[:space:]]+' 'NR==FNR {a[$1]=$2; next} $1 in a {print a[$1], $0}' file01.tab file02.tab

Output:

PGEN_.00g000010 Q86IC9;Q552T5                                                omt5
PGEN_.00g000020 P04177                                                       Th
PGEN_.00g000050 Q8L840;O04092;Q9FT71                                         RECQL4A
PGEN_.00g000060 Q61043;A0A1Y7VJL5;B2RQ73;B7ZMZ9;E9Q488;E9Q4S3;Q674R4;Q6ZPM7  Nin
PGEN_.00g000080 A1E2V0                                                       BIRC3
PGEN_.00g000090 P34456                                                       Uncharacterized
PGEN_.00g000120 P34457                                                       uncharacterized
PGEN_.00g000210 O00463;B4DIS9;B4E0A2;Q6FHY1                                  TRAF5
PGEN_.00g000230 Q00945                                                       RING
PGEN_.00g000240 Q5SWK7;Q8BXX5;Q9CXG1                                         Rnf145
  • FS='[;[:space:]]+' splits the line on a sequence of semicolons or space
    characters.
  • I have switched the order of files for simplicity.
甜嗑 2025-01-28 23:08:06

您可以尝试

awk 'NR==FNR{
        n=split($1,s,";"); 
        for(i=1;i<=n;++i) a[s[i]]=$0; 
        next;
    } ($1 in a){print $2,a[$1];}
' file02.tab file01.tab

你得到,

PGEN_.00g000010 Q86IC9;Q552T5                                                omt5
PGEN_.00g000020 P04177                                                       Th
PGEN_.00g000050 Q8L840;O04092;Q9FT71                                         RECQL4A
PGEN_.00g000060 Q61043;A0A1Y7VJL5;B2RQ73;B7ZMZ9;E9Q488;E9Q4S3;Q674R4;Q6ZPM7  Nin
PGEN_.00g000080 A1E2V0                                                       BIRC3
PGEN_.00g000090 P34456                                                       Uncharacterized
PGEN_.00g000120 P34457                                                       uncharacterized
PGEN_.00g000210 O00463;B4DIS9;B4E0A2;Q6FHY1                                  TRAF5
PGEN_.00g000230 Q00945                                                       RING
PGEN_.00g000240 Q5SWK7;Q8BXX5;Q9CXG1                                         Rnf145

you can try with split in awk

awk 'NR==FNR{
        n=split($1,s,";"); 
        for(i=1;i<=n;++i) a[s[i]]=$0; 
        next;
    } ($1 in a){print $2,a[$1];}
' file02.tab file01.tab

you get,

PGEN_.00g000010 Q86IC9;Q552T5                                                omt5
PGEN_.00g000020 P04177                                                       Th
PGEN_.00g000050 Q8L840;O04092;Q9FT71                                         RECQL4A
PGEN_.00g000060 Q61043;A0A1Y7VJL5;B2RQ73;B7ZMZ9;E9Q488;E9Q4S3;Q674R4;Q6ZPM7  Nin
PGEN_.00g000080 A1E2V0                                                       BIRC3
PGEN_.00g000090 P34456                                                       Uncharacterized
PGEN_.00g000120 P34457                                                       uncharacterized
PGEN_.00g000210 O00463;B4DIS9;B4E0A2;Q6FHY1                                  TRAF5
PGEN_.00g000230 Q00945                                                       RING
PGEN_.00g000240 Q5SWK7;Q8BXX5;Q9CXG1                                         Rnf145
逐鹿 2025-01-28 23:08:06

如果这仅是关于匹配半符号划界字符串中的第一个值,则还可以使用split并比较第一个值:

awk 'NR==FNR{split($1,p,";");a[p[1]]=$0; next} ($1 in a){print $2,a[$1]}' file02.tab file01.tab

或删除所有从第一个分号开始:

awk 'NR==FNR{x=$1;sub(/;.*/, "", x);a[x]=$0; next} ($1 in a){print $2,a[$1]}' file02.tab file01.tab

输出

PGEN_.00g000010 Q86IC9;Q552T5                                                omt5
PGEN_.00g000020 P04177                                                       Th
PGEN_.00g000050 Q8L840;O04092;Q9FT71                                         RECQL4A
PGEN_.00g000060 Q61043;A0A1Y7VJL5;B2RQ73;B7ZMZ9;E9Q488;E9Q4S3;Q674R4;Q6ZPM7  Nin
PGEN_.00g000080 A1E2V0                                                       BIRC3
PGEN_.00g000090 P34456                                                       Uncharacterized
PGEN_.00g000120 P34457                                                       uncharacterized
PGEN_.00g000210 O00463;B4DIS9;B4E0A2;Q6FHY1                                  TRAF5
PGEN_.00g000230 Q00945                                                       RING
PGEN_.00g000240 Q5SWK7;Q8BXX5;Q9CXG1                                         Rnf145

If this is only about matching the first value in the semicolon delimited string, you can also use split and compare the first value:

awk 'NR==FNR{split($1,p,";");a[p[1]]=$0; next} ($1 in a){print $2,a[$1]}' file02.tab file01.tab

Or removing all starting at the first semicolon:

awk 'NR==FNR{x=$1;sub(/;.*/, "", x);a[x]=$0; next} ($1 in a){print $2,a[$1]}' file02.tab file01.tab

Output

PGEN_.00g000010 Q86IC9;Q552T5                                                omt5
PGEN_.00g000020 P04177                                                       Th
PGEN_.00g000050 Q8L840;O04092;Q9FT71                                         RECQL4A
PGEN_.00g000060 Q61043;A0A1Y7VJL5;B2RQ73;B7ZMZ9;E9Q488;E9Q4S3;Q674R4;Q6ZPM7  Nin
PGEN_.00g000080 A1E2V0                                                       BIRC3
PGEN_.00g000090 P34456                                                       Uncharacterized
PGEN_.00g000120 P34457                                                       uncharacterized
PGEN_.00g000210 O00463;B4DIS9;B4E0A2;Q6FHY1                                  TRAF5
PGEN_.00g000230 Q00945                                                       RING
PGEN_.00g000240 Q5SWK7;Q8BXX5;Q9CXG1                                         Rnf145
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文