Peace be unto you o enlightened ones.

I have a large text file with many (~64000) similar lines in it. I would like to create a new file, differing from the original one in the following way:

If a line in the original file has 188 characters, followed by 8 numbers, followed by 16 characters, the corresponding line in the new file must have the 8 numbers replaced with 20020101, unless the original 8 numbers are 00000000, in which case the line is not copied into the new file.

I was thinking of something along the lines of:

perl -lne 'print unless /.{188}00000000.{16}/' original.txt > new.txt

This works for getting rid of the lines with 00000000 in them, but how do I adapt this in order to implement the replacement mentioned above?

Thanks in advance,

S


In reply to 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.