First of all, you probably want /^.{188}00000000.{16}$/ so that you match from the beginning to the end of the string... what you have could match a longer line that matched those criteria, if there was one. If the 8 digit number will never start with a zero (assuming it is already a date), then
perl -lne 'print if s/^(.{188})[1-9]\d{7}(.{16})$/${1}20020101$2/' ori +ginal.txt > new.txt
would work. Theoretically you could use lookahead and such to negate the need of capturing parens (Upd: As ishnid shows).

There is probably a way to do it in one regexp, but I can't think of it now, so if zero at the start is a possibility.

while(<>) { next unless /^(.{188})(\d{8})(.{16})$/; print $1,"20020101$3\n" unless $2 eq '00000000'; }
untested, but should work.

                - Ant
                - Some of my best work - (1 2 3)


In reply to Re: search and replace on a line by line basis by suaveant
in thread search and replace on a line by line basis by Anonymous Monk

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.