Update: this is an answer to the wrong question! It solves the problem of quoting only the first and last n/N runs. The OP wants to replace the first and last N/n in each run with ^.

The following (rather fussy because of special cases) code remembers where the last N/n run started then rewrites the last part of the output file with the markers inserted.

use strict; use warnings; my $data = <<DATA; acacccacacacaccacacccacacaccacacccacacccacacaccaca nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn cccacaccacacccacacaccacacaccacacccacacccacacacacca cacccacacaccacacccacacacaccctaaccctaacccctaaccccta accctaacccnnnnnnnnnnnnnnnnnnnnnnnnnnnccctaaccctaac ccctaaccctaaccctaaccgtaaccctaaccctttaccctaacccgaac ccctaacnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnggggg gaccctgaccgtgaccctgaccctaacccgaacccgaacccgaaccccga accccgaaccccgaaccccaaccccaaccccaaccccaaccctaacccct caccctcaccctcgacccccgacccccgacccccgacccccaccccgaac ggnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnaccctaaccctaaaaccctaaccctagcc ctagccctagccctagccctaacccctaacccctaaccctaagccgaagc DATA my $result; open my $inFile, '<', \$data or die "Unable to open input fille: $!\n +"; open my $outFile, '>', \$result or die "Unable to create output file: +$1\n"; my $currStartN; my $lastStartN; my $lastEndN; my $prevLineEnd = 0; while (<$inFile>) { my $runEnd; if (defined $currStartN) { # Looking for an end N/n next unless /([^nN\n])/; # Continue if full line of N/n $runEnd = $-[0]; # Insert ^ for first run if ($runEnd == 0) { # Need to insert ^ at end of previous line seek $outFile, $prevLineEnd, 0; print $outFile "^\n"; } else { substr $_, $runEnd, 0, '^' unless defined $lastStartN; } } else { # Looking for a start N/n next unless /([nN]+)/; $runEnd = $+[0]; $currStartN = tell ($inFile) - length ($_) + $-[0]; s/([nN])/^$1/ unless defined $lastStartN; # Insert ^ for first + run next unless $runEnd < length ($_) - 2; # Continue if full line + of N/n } $lastStartN = $currStartN; $lastEndN = tell ($inFile) - length ($_) + $runEnd; $currStartN = undef; } continue { $prevLineEnd = tell $outFile; print $outFile $_; chomp; $prevLineEnd += length; } # Insert ^ around last run of N/n seek $outFile, $lastStartN + 2, 0; print $outFile '^'; seek $inFile, $lastStartN, 0; read $inFile, my $nRun, ($lastEndN - $lastStartN - 1); print $outFile $nRun, '^'; print $outFile $_ while <$inFile>; close $inFile; close $outFile; print $result;

Prints:

acacccacacacaccacacccacacaccacacccacacccacacaccaca ^nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn^ cccacaccacacccacacaccacacaccacacccacacccacacacacca cacccacacaccacacccacacacaccctaaccctaacccctaaccccta accctaacccnnnnnnnnnnnnnnnnnnnnnnnnnnnccctaaccctaac ccctaaccctaaccctaaccgtaaccctaaccctttaccctaacccgaac ccctaacnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnggggg gaccctgaccgtgaccctgaccctaacccgaacccgaacccgaaccccga accccgaaccccgaaccccaaccccaaccccaaccccaaccctaacccct caccctcaccctcgacccccgacccccgacccccgacccccaccccgaac gg^nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnn^naccctaaccctaaaaccctaaccctagcc ctagccctagccctagccctaacccctaacccctaaccctaagccgaagc

Perl is environmentally friendly - it saves trees

In reply to Re: replace fist and last occurrences of N by GrandFather
in thread replace fist and last occurrences of N by ini2005

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.