使用perl将文件中的模式替换为该特定列的标题(Replace a pattern in file with the header of that specific column with perl)

编程入门 行业动态 更新时间:2024-10-24 12:24:49
使用perl将文件中的模式替换为该特定列的标题(Replace a pattern in file with the header of that specific column with perl)

我正在使用以下文件格式:

POS1 POS2 ID1 ID2 ID3 ID4 … 1 ### 0/0:.:58:.:. 0/0:.:51:.:. 0/0:.:104:.:. 0/0:.:16:.:. 2 ### 0/0:89:.:.:. 0/0:75:.:.:. 0/0:125:.:.:. 0/0:57:.:.:. 3 ### 0/0:.:93:.:. 0/0:.:85:.:. 0/0:.:127:.:. 0/0:.:60:.:. 4 ### .:.:.:.:. 0/0:7:.:.:. .:.:.:.:. .:.:.:.:. 5 ### .:.:.:.:. .:.:.:.:. .:.:.:.:. .:.:.:.:. 6 ### .:.:.:.:. 0/0:8:.:.:. .:.:.:.:. .:.:.:.:. 7 ### 0/0:24:.:.:. 0/0:41:.:.:. 0/0:17:.:.:. 0/0:33:.:.:. 8 ### 0/0:39:.:.:. 0/0:59:.:.:. 0/1:68:.:.:. 0/0:77:.:.:. 9 ### 0/0:.:18:.:. 0/0:.:36:.:. 0/0:.:26:.:. 0/0:.:38:.:. 10 ### 0/0:.:18:.:. 0/0:.:32:.:. 0/0:.:24:.:. 0/0:.:34:.:. 11 ### 0/1:18:.:.:. 0/0:28:.:.:. 0/0:24:.:.:. 0/0:34:.:.:. 12 ### 0/0:.:13:.:. 0/0:.:22:.:. 0/0:.:23:.:. 0/0:.:35:.:. 13 ### 0/0:.:25:.:. 0/0:.:30:.:. 0/0:.:34:.:. 0/0:.:38:.:. 14 ### 0/0:20:.:.:. 0/0:28:.:.:. 0/0:34:.:.:. 0/0:39:.:.:. 15 ### 0/0:19:.:.:. 0/0:27:.:.:. 0/0:32:.:.:. 0/0:39:.:.:. 16 ### .:.:.:.:. 0/0:26:.:.:. 0/1:34:.:.:. 0/0:32:.:.:. 17 ### .:.:.:.:. .:.:.:.:. 0/0:.:9:.:. 0/0:.:13:.:.

我遇到的问题是尝试列出包含“0/1”模式的每个位置的ID。 即使你们只能弄清楚如何用特定的列ID替换“0/1”,我也可以自己完成剩下的工作。 列和行的数量变化很大,可能是数千。

不同的公式:如果一个位置在该行中包含一个带有“0/1”的ID,它应该列出它找到“0/1”的列的列ID

是否有可能使用perl(或bash)解决这个问题以及如何解决? 我仍然是这种语言的新手,只知道它只有4周。

我向前谢谢你。

EDIT示例输出应该是这样的:

POS1 POS2 8 ### ID3 11 ### ID1 16 ### ID3

如果多个列包含0/1,那么该位置应该是所有ID。 对不起,如果我听起来有点混乱,在办公室度过漫长的一天。

I'm working with the following file format:

POS1 POS2 ID1 ID2 ID3 ID4 … 1 ### 0/0:.:58:.:. 0/0:.:51:.:. 0/0:.:104:.:. 0/0:.:16:.:. 2 ### 0/0:89:.:.:. 0/0:75:.:.:. 0/0:125:.:.:. 0/0:57:.:.:. 3 ### 0/0:.:93:.:. 0/0:.:85:.:. 0/0:.:127:.:. 0/0:.:60:.:. 4 ### .:.:.:.:. 0/0:7:.:.:. .:.:.:.:. .:.:.:.:. 5 ### .:.:.:.:. .:.:.:.:. .:.:.:.:. .:.:.:.:. 6 ### .:.:.:.:. 0/0:8:.:.:. .:.:.:.:. .:.:.:.:. 7 ### 0/0:24:.:.:. 0/0:41:.:.:. 0/0:17:.:.:. 0/0:33:.:.:. 8 ### 0/0:39:.:.:. 0/0:59:.:.:. 0/1:68:.:.:. 0/0:77:.:.:. 9 ### 0/0:.:18:.:. 0/0:.:36:.:. 0/0:.:26:.:. 0/0:.:38:.:. 10 ### 0/0:.:18:.:. 0/0:.:32:.:. 0/0:.:24:.:. 0/0:.:34:.:. 11 ### 0/1:18:.:.:. 0/0:28:.:.:. 0/0:24:.:.:. 0/0:34:.:.:. 12 ### 0/0:.:13:.:. 0/0:.:22:.:. 0/0:.:23:.:. 0/0:.:35:.:. 13 ### 0/0:.:25:.:. 0/0:.:30:.:. 0/0:.:34:.:. 0/0:.:38:.:. 14 ### 0/0:20:.:.:. 0/0:28:.:.:. 0/0:34:.:.:. 0/0:39:.:.:. 15 ### 0/0:19:.:.:. 0/0:27:.:.:. 0/0:32:.:.:. 0/0:39:.:.:. 16 ### .:.:.:.:. 0/0:26:.:.:. 0/1:34:.:.:. 0/0:32:.:.:. 17 ### .:.:.:.:. .:.:.:.:. 0/0:.:9:.:. 0/0:.:13:.:.

The problem I'm having is trying to list the ID's per position that contain a "0/1" pattern. Even if you guys could only figure out how I could replace the "0/1" with that specific column ID, I could work the rest out myself. The amount of columns and rows are very variable, possible thousands.

Differently formulated: If a position contains an ID with a "0/1" in that row it should list the column ID of the column it found the "0/1" in

Is it possible to work through this problem with perl (or bash) and how? I'm still somewhat of a newbie at the language, having known it for only 4 weeks.

My thanks in forward.

EDIT Sample output should be something like this:

POS1 POS2 8 ### ID3 11 ### ID1 16 ### ID3

If more than one columns contain a 0/1 it should all ID's for that position. Sorry if I sound a bit confusing, long day at the office.

最满意答案

我想你想要这样的东西? 程序期望输入文件作为命令行上的参数。

use strict; use warnings; my @headers; while (<>) { chomp; my @fields = split /\t/; if (@headers) { my @ids; for my $i (2 .. $#fields) { push @ids, $headers[$i] if $fields[$i] =~ m:0/1:; } print join("\t", $fields[0], @ids), "\n" if @ids; } else { @headers = @fields; print join("\t", @headers[0,1]), "\n"; } }

产量

POS1 POS2 8 ID3 11 ID1 16 ID3

I think you want something like this? The program expects the input file as a parameter on the command line.

use strict; use warnings; my @headers; while (<>) { chomp; my @fields = split /\t/; if (@headers) { my @ids; for my $i (2 .. $#fields) { push @ids, $headers[$i] if $fields[$i] =~ m:0/1:; } print join("\t", $fields[0], @ids), "\n" if @ids; } else { @headers = @fields; print join("\t", @headers[0,1]), "\n"; } }

output

POS1 POS2 8 ID3 11 ID1 16 ID3

更多推荐

本文发布于:2023-08-07 06:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462626.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为该   模式   标题   文件   perl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!