#!/usr/bin/env perl use strict; use warnings; my $content = join '', ; my ($header, undef) = split /\a\a/, $content, 2; my $cols = scalar split /\a/, $header; my $re = qr{((?:(?:|[^\a]+)\a){$cols}\a)}; { print "*** WANTED ***\n"; while ($content =~ /$re/g) { my $row = $1; $row =~ s/\a/(BEL)/g; print "$row\n"; } } { print "\n*** PROBABLY MORE USEFUL ***\n"; my @rows; while ($content =~ /$re/g) { my $row = $1; $row =~ s/\a$//; push @rows, [ split /\a/, $row ]; } print join('|', @$_), "\n" for @rows; } __DATA__ Agreement^GACAP^GACAP^GAccord^G^Galbatross^G^G^Galbatros^G^Galleged violation^G^G^Ginfraction présumée^G^Gallowable^G^G^Gadmissible^G^Ganchovy^G^G^Ganchois^G^Gangler fish, burbot^G^G^Glotte^G^G