in reply to search and replace on a line by line basis

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)

Replies are listed 'Best First'.
Re: Re: search and replace on a line by line basis
by MidLifeXis (Monsignor) on Apr 15, 2004 at 17:10 UTC

    Or to minimize it one more character....

    s/^(.{188})&#91;^0&#93;\d{7}(.{16})$/${1}20020101$2/'

    Update: As the AM below notes, this does not solve the original question, just minimizes the previous poster's RE.

      that ^0 will skip anything starts with 0. it's not what the poster want.

      how about s/^(.{188})(?!0(8))(.{16})$/${1}20020101$4/