I would use a loop to parse the input...
The output is:use strict; use warnings; my $chunk = 0; my $flag = 0; open my $fh, '<', "data.txt"; while (my $line = <$fh>) { chomp $line; my ($code, $state) = split / +/, $line; if ($flag and $state eq 'False') { print "\n#Chunk $chunk\n"; $chunk++; } print "$line\n"; $flag = 1; }
C:\Users\user\Perl>perl test.pl GO:0002366 False GO:0002446 True GO:0002275 True GO:0043312 True #Chunk 0 GO:0006733 False #Chunk 1 GO:0019674 False #Chunk 2 GO:0043588 False #Chunk 3 GO:0055065 False GO:0055080 True
UPDATE: I've just noticed you want #Chunk 0 at the top so no $flag is needed to suppress it...
use strict; use warnings; my $chunk = 0; open my $fh, '<', "data.txt"; while (my $line = <$fh>) { chomp $line; my ($code, $state) = split / +/, $line; if ($state eq 'False') { print "\n#Chunk $chunk\n"; $chunk++; } print "$line\n"; }
Output:
C:\Users\user\Perl>perl test.pl #Chunk 0 GO:0002366 False GO:0002446 True GO:0002275 True GO:0043312 True #Chunk 1 GO:0006733 False #Chunk 2 GO:0019674 False #Chunk 3 GO:0043588 False #Chunk 4 GO:0055065 False GO:0055080 True C:\Users\user\Perl>
In reply to Re: Parsing block of texts
by Bod
in thread Parsing block of texts
by perlance
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |