Hello Saved,

Generally is a bad idea to do an inplace edit without backup. For this reason Perl -i commandline switch always ask for a backup file extension.

Infact a simple oneliner (pay attention to windows doublequotes around oneliner!) can do the basic work you need to get started:

cat add-data.txt 432 10TH ST APT (Range 2A - 2B) BROOKLYN NY 10598-6601 432 10TH ST APT (Range 3A - 3B) BROOKLYN NY 10598-6601 432 10TH ST APT (Range 4A - 4B) BROOKLYN NY 10598-6605 432 10TH ST APT (Range 5A - 5D) BROOKLYN NY 10598-6605 432 10TH ST APT 6A BROOKLYN NY 10598-6605 perl -i.bak -p -e "s/BROOKLYN/BROCCOLINO/ and $c++; END{print qq($c ch +anges\n)}" add-data.txt 5 changes cat add-data.txt 432 10TH ST APT (Range 2A - 2B) BROCCOLINO NY 10598-6601 432 10TH ST APT (Range 3A - 3B) BROCCOLINO NY 10598-6601 432 10TH ST APT (Range 4A - 4B) BROCCOLINO NY 10598-6605 432 10TH ST APT (Range 5A - 5D) BROCCOLINO NY 10598-6605 432 10TH ST APT 6A BROCCOLINO NY 10598-6605

You dont need the two times say in your original program: just do the replacement (add to counter if it happens) and write the line to the file (changed or not).

To get a longer and more complex program it would be better to use Getopt::Long to grab your arguments: program.pl -f file.txt --backup file.txt.bak --match BROOKLYN --replace BROCCOLINO

You can benefit from qr to get your pattern compiled (and you can check there for pattern compilation errors).

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: user input Regular Expression -- oneliner by Discipulus
in thread user input Regular Expression by Saved

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.