由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Unix版 - Perl: conditionally match multiple lines
相关主题
reverse the lines?shell programming
a simple perl problem?CGI之Perl Script一问
awk questionhow to let awk not print?
vi tutorial 7how to write robort in perl w/out expect?
multiple slashes don't hurtHow to do "which" in perl?
Re: 请推荐一本perl book好不好how to run active perl in cygwin?
好马配好鞍/介绍一套linux牛书/O'Reilly Animal Book[转载] Perl 土问题
how to turn off sendmail[转载] Perl高手请进
相关话题的讨论汇总
话题: lines话题: match话题: multiple话题: read
进入Unix版参与讨论
1 (共1页)
r*****o
发帖数: 28
1
I only know how to read a file line by line
or read in the whole file as a string.
But how to read/match multiple lines conditionally?
e.g. a file contains certain lables:
xxx
yyy
Begin: zzz
aaa
bbb
Ene: ccc
ddd
I need the script to read/match multiple lines between the Begin/End
pair as a single string. But for other lines, just read
them one by one.
Is there any easy way to do this?
Thanks.
l*****k
发帖数: 587
2
here is part of my code for this:
# it searches to the >$name, which is the alignment part for that hit
# it reads all info till the next > to $&
# $result get this info for further process
#
while($align=~ /^>$name.*\n(^(?!>).*\n)+/gm) {
$result = $&;
for your purpose, just change the second > into end, first > into begin, I bet
it should work.
and I am sure you are doing some sequence pars

【在 r*****o 的大作中提到】
: I only know how to read a file line by line
: or read in the whole file as a string.
: But how to read/match multiple lines conditionally?
: e.g. a file contains certain lables:
: xxx
: yyy
: Begin: zzz
: aaa
: bbb
: Ene: ccc

r*****o
发帖数: 28
3
Sorry, I am a novice on Perl and don't quite understand your script.
I assume the $align is the big string for a whole file.
And I think it works to strip out multiple lines in a certain block,
but what about other lines? Can it read them out one by one?
(I think if only to match out certain blocks, isn't .. or ... operators
a better way?)
Another question is how to match nested blocks? e.g.
(block ...
(block ...
...
(block ...
))
)
Is there any way to match the whole outer bloc

【在 l*****k 的大作中提到】
: here is part of my code for this:
: # it searches to the >$name, which is the alignment part for that hit
: # it reads all info till the next > to $&
: # $result get this info for further process
: #
: while($align=~ /^>$name.*\n(^(?!>).*\n)+/gm) {
: $result = $&;
: for your purpose, just change the second > into end, first > into begin, I bet
: it should work.
: and I am sure you are doing some sequence pars

r*****o
发帖数: 28
4
After lots of experiments and head-scratch,
I think I can now match the out-most block
(For clarity, I write it in multiple lines:
m{\(
.*?
\( [^\)]*
\)
}gx
1 (共1页)
进入Unix版参与讨论
相关主题
[转载] Perl高手请进multiple slashes don't hurt
[转载] QUestion about PerlRe: 请推荐一本perl book好不好
怎么在FTP里改变文件属性?好马配好鞍/介绍一套linux牛书/O'Reilly Animal Book
[转载] 请教三个关于emacs和perl的问题how to turn off sendmail
reverse the lines?shell programming
a simple perl problem?CGI之Perl Script一问
awk questionhow to let awk not print?
vi tutorial 7how to write robort in perl w/out expect?
相关话题的讨论汇总
话题: lines话题: match话题: multiple话题: read