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*
In reply to Re: user input Regular Expression -- oneliner
by Discipulus
in thread user input Regular Expression
by Saved
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |