... my $seq = <$fh>; # entire file is now in $seq; $seq =~ tr/\n//d; # remove all newlines open( my $out, ">", "write.txt" ) or die "Cannot create write.txt: $!\n"; my @chunks = split /(..$find..)/, $seq; my $offset = 0; for my $chunk ( @chunks ) { if ( $chunk =~ /(..)$find(..)/ ) { printf( "%s occurs at character offset %d, between %s and %s\n", $find, $offset + 2, $1, $2 ); } $offset += length( $chunk ); }