Help: Process text block

A

Amy Lee

Hello,

I have many files fill with following contents.

10 dG = -27.9 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 0 4 0 0
5 U 4 6 12 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 45 8 0 0
9 A 8 10 46 9 0 0
10 U 9 11 0 10 0 0
10 dG = -27.3 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 0 4 0 0
5 U 4 6 35 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 16 8 0 0
9 A 8 10 0 9 0 0
10 U 9 11 0 10 0 0
10 dG = -27.2 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 71 4 0 0
5 U 4 6 0 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 0 8 0 9
9 A 8 10 128 9 8 10
10 U 9 11 127 10 9 11

The text block is from "dG" to the next "dG". And further I just process
the first block. So that's following contents.

1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 0 4 0 0
5 U 4 6 12 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 45 8 0 0
9 A 8 10 46 9 0 0
10 U 9 11 0 10 0 0

I don't know how to write such condition to do that. I used "while" loop
to do that but still failed. So could anyone show me some points?

Thank you very much.

Best Regards.

Amy Lee
 
T

Tad J McClellan

The text block is from "dG" to the next "dG". And further I just process
the first block.

I don't know how to write such condition to do that. I used "while" loop
to do that but still failed. So could anyone show me some points?


--------------------------------------
#!/usr/bin/perl
use warnings;
use strict;

my $buffer = '';
while ( <DATA> ) {
if ( /dG = / and $buffer ) {
process_block($buffer);
$buffer = '';
}
$buffer .= $_;
}
process_block($buffer);

sub process_block { print "(($_[0]))\n\n" }


__DATA__
10 dG = -27.9 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 0 4 0 0
5 U 4 6 12 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 45 8 0 0
9 A 8 10 46 9 0 0
10 U 9 11 0 10 0 0
10 dG = -27.3 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 0 4 0 0
5 U 4 6 35 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 16 8 0 0
9 A 8 10 0 9 0 0
10 U 9 11 0 10 0 0
10 dG = -27.2 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 71 4 0 0
5 U 4 6 0 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 0 8 0 9
9 A 8 10 128 9 8 10
10 U 9 11 127 10 9 11
 
J

John W. Krahn

Amy said:
Hello,

I have many files fill with following contents.

10 dG = -27.9 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 0 4 0 0
5 U 4 6 12 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 45 8 0 0
9 A 8 10 46 9 0 0
10 U 9 11 0 10 0 0
10 dG = -27.3 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 0 4 0 0
5 U 4 6 35 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 16 8 0 0
9 A 8 10 0 9 0 0
10 U 9 11 0 10 0 0
10 dG = -27.2 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 71 4 0 0
5 U 4 6 0 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 0 8 0 9
9 A 8 10 128 9 8 10
10 U 9 11 127 10 9 11

The text block is from "dG" to the next "dG". And further I just process
the first block. So that's following contents.

1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 0 4 0 0
5 U 4 6 12 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 45 8 0 0
9 A 8 10 46 9 0 0
10 U 9 11 0 10 0 0

I don't know how to write such condition to do that. I used "while" loop
to do that but still failed. So could anyone show me some points?

$ echo "
10 dG = -27.9 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 0 4 0 0
5 U 4 6 12 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 45 8 0 0
9 A 8 10 46 9 0 0
10 U 9 11 0 10 0 0
10 dG = -27.3 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 0 4 0 0
5 U 4 6 35 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 16 8 0 0
9 A 8 10 0 9 0 0
10 U 9 11 0 10 0 0
10 dG = -27.2 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 71 4 0 0
5 U 4 6 0 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 0 8 0 9
9 A 8 10 128 9 8 10
10 U 9 11 127 10 9 11
" | perl -ne'print if ?dG? ... /dG/ and ! /dG/'
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 0 4 0 0
5 U 4 6 12 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 45 8 0 0
9 A 8 10 46 9 0 0
10 U 9 11 0 10 0 0




John
 
A

Amy Lee

$ echo "
10 dG = -27.9 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 0 4 0 0
5 U 4 6 12 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 45 8 0 0
9 A 8 10 46 9 0 0
10 U 9 11 0 10 0 0
10 dG = -27.3 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 0 4 0 0
5 U 4 6 35 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 16 8 0 0
9 A 8 10 0 9 0 0
10 U 9 11 0 10 0 0
10 dG = -27.2 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 71 4 0 0
5 U 4 6 0 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 0 8 0 9
9 A 8 10 128 9 8 10
10 U 9 11 127 10 9 11
" | perl -ne'print if ?dG? ... /dG/ and ! /dG/'
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 0 4 0 0
5 U 4 6 12 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 45 8 0 0
9 A 8 10 46 9 0 0
10 U 9 11 0 10 0 0




John
Thank you very much, anyway, how to write this into codes? Especially ?dG?
.... /dG/ part.

Amy Lee
 
A

Amy Lee

Thank you very much, anyway, how to write this into codes? Especially ?dG?
... /dG/ part.

Amy Lee
And furthermore, could you explain the codes? I didn't understand it
clearly. Thank you very much.
 
A

Amy Lee

The text block is from "dG" to the next "dG". And further I just process
the first block.

I don't know how to write such condition to do that. I used "while" loop
to do that but still failed. So could anyone show me some points?


--------------------------------------
#!/usr/bin/perl
use warnings;
use strict;

my $buffer = '';
while ( <DATA> ) {
if ( /dG = / and $buffer ) {
process_block($buffer);
$buffer = '';
}
$buffer .= $_;
}
process_block($buffer);

sub process_block { print "(($_[0]))\n\n" }


__DATA__
10 dG = -27.9 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 0 4 0 0
5 U 4 6 12 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 45 8 0 0
9 A 8 10 46 9 0 0
10 U 9 11 0 10 0 0
10 dG = -27.3 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 0 4 0 0
5 U 4 6 35 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 16 8 0 0
9 A 8 10 0 9 0 0
10 U 9 11 0 10 0 0
10 dG = -27.2 Gm01_9996700_9996828_gma-MIR1520b
1 U 0 2 0 1 0 0
2 U 1 3 0 2 0 0
3 U 2 4 0 3 0 0
4 C 3 5 71 4 0 0
5 U 4 6 0 5 0 0
6 A 5 7 0 6 0 0
7 A 6 8 0 7 0 0
8 A 7 9 0 8 0 9
9 A 8 10 128 9 8 10
10 U 9 11 127 10 9 11
--------------------------------------
Thank you very much.

Amy
 
J

John W. Krahn

[ *SNIP* ]

[ *SNIP* ]

[ *SNIP* ]

[ *SNIP* ]

$ perl -MO=Deparse -ne'print if ?dG? ... /dG/ and ! /dG/'
LINE: while (defined($_ = <ARGV>)) {
print $_ if ?dG? ... /dG/ and not /dG/;
}
-e syntax OK


Or:

while ( <FILEHANDLE> ) {
if ( ?dG? ... /dG/ ) {
print unless /dG/;
}
}

And furthermore, could you explain the codes? I didn't understand it
clearly. Thank you very much.

?dG? only matches the first time it is found while /dG/ will match every
time it is found. '...' is the flip-flop operator. The expression is
false until the left operand is true and then it is true until the right
operand is true when it becomes false again. So it matches the first
line that contains 'dG' until the next line that contains 'dG' and then
quits looking. And once it has found the correct line(s), you don't
want to print the line that contains 'dG', just the other lines.



John
 
A

Amy Lee

[ *SNIP* ]

[ *SNIP* ]

[ *SNIP* ]

[ *SNIP* ]

$ perl -MO=Deparse -ne'print if ?dG? ... /dG/ and ! /dG/'
LINE: while (defined($_ = <ARGV>)) {
print $_ if ?dG? ... /dG/ and not /dG/;
}
-e syntax OK


Or:

while ( <FILEHANDLE> ) {
if ( ?dG? ... /dG/ ) {
print unless /dG/;
}
}

And furthermore, could you explain the codes? I didn't understand it
clearly. Thank you very much.

?dG? only matches the first time it is found while /dG/ will match every
time it is found. '...' is the flip-flop operator. The expression is
false until the left operand is true and then it is true until the right
operand is true when it becomes false again. So it matches the first
line that contains 'dG' until the next line that contains 'dG' and then
quits looking. And once it has found the correct line(s), you don't
want to print the line that contains 'dG', just the other lines.



John
Thank you sir. However, when I use such following codes.

while (<>)
{
if (?dG? ... /dG/ && !/dG/)
{
print;
}
}
Still failed. So I don't know why it could happen, however if I change if
loop into if ((?dG? ... /dG/) && (!/dG/)) that could be available. So what
I wanna ask is the reason for such situations.

Thank you sir.

Amy
 
T

Tad J McClellan

[ snip a full quote ]
Thank you sir.


DO NOT QUOTE ENTIRE ARTICLES!!

This is seen as rude, and I'm sure you do not want to be seen
as rude, so please stop doing that.

If you have a question about 5 lines of code, then quote only
those 5 lines of code, not the entire program that the lines appear in.

if (?dG? ... /dG/ && !/dG/)
Still failed. So I don't know why it could happen, however if I change if
loop into if ((?dG? ... /dG/) && (!/dG/)) that could be available. So what
I wanna ask is the reason for such situations.


Precedence.


perldoc perlop
 
J

John W. Krahn

Amy said:
Amy said:
On Mon, 26 Jan 2009 01:41:02 +0800, Amy Lee wrote:

On Sun, 25 Jan 2009 08:14:00 -0800, John W. Krahn wrote:

Amy Lee wrote:
I have many files fill with following contents.
[ *SNIP* ]
The text block is from "dG" to the next "dG". And further I just process
the first block. So that's following contents.
[ *SNIP* ]
I don't know how to write such condition to do that. I used "while" loop
to do that but still failed. So could anyone show me some points?
[ *SNIP* ]
" | perl -ne'print if ?dG? ... /dG/ and ! /dG/'
[ *SNIP* ]
Thank you very much, anyway, how to write this into codes? Especially ?dG?
... /dG/ part.
$ perl -MO=Deparse -ne'print if ?dG? ... /dG/ and ! /dG/'
LINE: while (defined($_ = <ARGV>)) {
print $_ if ?dG? ... /dG/ and not /dG/;
}
-e syntax OK


Or:

while ( <FILEHANDLE> ) {
if ( ?dG? ... /dG/ ) {
print unless /dG/;
}
}

And furthermore, could you explain the codes? I didn't understand it
clearly. Thank you very much.
?dG? only matches the first time it is found while /dG/ will match every
time it is found. '...' is the flip-flop operator. The expression is
false until the left operand is true and then it is true until the right
operand is true when it becomes false again. So it matches the first
line that contains 'dG' until the next line that contains 'dG' and then
quits looking. And once it has found the correct line(s), you don't
want to print the line that contains 'dG', just the other lines.

Thank you sir. However, when I use such following codes.

while (<>)
{
if (?dG? ... /dG/ && !/dG/)
{
print;
}
}
Still failed. So I don't know why it could happen, however if I change if
loop into if ((?dG? ... /dG/) && (!/dG/)) that could be available. So what
I wanna ask is the reason for such situations.

The reason I used 'and' instead of '&&' is exactly because of the high
precedence of '&&' which, as you say, doesn't work unless you use
parentheses. If you are going to change the code I posted you have to
expect different results sometimes.



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top