在 Perl 中,如何读取符合条件的部分行? [英] In Perl, how can I read parts of lines that match a criterion?
问题描述
样本数据:
603 Some garbage data not related to me, 55, 113 ->
1-ENST0000 This is sample data blh blah blah blahhhh
2-ENSBTAP0 This is also some other sample data
21-ENADT)$ DO NOT WANT TO READ THIS LINE.
3-ENSGALP0 This is third sample data
node #4 This is 4th sample data
node #5 This is 5th sample data
This is also part of the input file but i dont wish to read this.
Branch -> 05 13,
44, 1,1,4,1
17, 1150
637 YYYYYY: 2 : %
在上述数据中.这些部分的列宽是固定的,但可能有些部分我不想阅读.上面的示例数据已被编辑以反映这一点.
In the above data. The column width is fixed for the sections but there might be some sections I do not wish to read. above sample data has been edited to reflect that.
所以在这个输入文件中,我想将第一部分1-ENST0000"的内容读入一个数组,将2-ENSBTAP0"的内容读入一个单独的数组,依此类推.
So in this input file I want to read contents of first section '1-ENST0000' into an array and contents of '2-ENSBTAP0' into a separate array and so on.
我在想出一个定义模式的正则表达式时遇到了麻烦……前三行有
然后也可以有 node#<这里有一些数字>
I am having trouble coming up with a regex that will define the pattern ...first three lines have <someNumber>-ENS<someotherstuf>
and then there can also be node #<some number here>
推荐答案
好的,根据您后来的评论,这与上一个问题有点不同.另外,我现在意识到 node #54
是第一列中的有效条目.
OK, based on your later comment, this is a little different than the previous question. Also, I now realize that node #54
is a valid entry in the first column.
更新:我现在也意识到您不需要第一列.
Update: I now also realize you do not need the first column.
更新:一般来说,您既不想也不需要在 Perl 中处理字符数组.
Update: In general, you neither want to nor need to deal with character arrays in Perl.
更新: 现在您已经阐明了应该和不应该跳过的内容,这里有一个处理这个问题的版本.在 if
条件中添加要品尝的模式.
Update: Now that you clarified the what should and should not be skipped, here is a version that deals with that. Add patterns to taste in the if
condition.
#!/usr/bin/perl
use strict;
use warnings;
my @data;
while ( <DATA> ) {
chomp;
if ( /^[0-9]+-ENS.{5} +(.+)$/
or /^node #[0-9]+ +(.+)$/
) {
push @data, [ split //, $1 ];
}
}
use Data::Dumper;
print Dumper \@data;
__DATA__
603 Some garbage data not related to me, 55, 113 ->
1-ENST0000 This is sample data blh blah blah blahhhh
2-ENSBTAP0 This is also some other sample data
21-ENADT)$ DO NOT WANT TO READ THIS LINE.
3-ENSGALP0 This is third sample data
node #4 This is 4th sample data
node #5 This is 5th sample data
This is also part of the input file but i dont wish to read this.
Branch -> 05 13,
44, 1,1,4,1
17, 1150
637 YYYYYY: 2 : %
至于学习如何钓鱼,我建议您阅读 perldoc perltoc 中的所有相关内容.
As for learning how to fish, I recommend you read everything related in perldoc perltoc.
这篇关于在 Perl 中,如何读取符合条件的部分行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!