|Keep It Simple, Stupid|
How to handle metacharacters in input file for Perl one-liner codeby debug (Initiate)
|on Jul 16, 2015 at 12:47 UTC||Need Help??|
debug has asked for the wisdom of the Perl Monks concerning the following question:
Hello monks!! This is my first post here, even though I've been using PerlMonks gems as lifesavers for a while now :)
First, my problem code:
my $file = "foo.c";
`perl -p -l -i.bak -e "s/$file/$file,=SUM(B$x:B$y)/" sample.csv`
sample.csv has a list of C filenames in column A. Based on certain conditions, I want to display a sum of values in the same row as a filename match in its equivalent column B.
My problem is that while searching for foo.c, the code also matches and replaces files like foo_con.c, foo_con_bar.c etc. I'm not sure why, but its obvious that my . separator in the filename is somehow being ignored even when the text has an _ in its place, leading to multiple invalid substitutions i.e. foo_c is also detected as a valid string match.
I would like to know if there is any way to make the code detect the . separator and not mistake it with an _ in other filenames.
Notice that I do not have a /g global modifier in my code string. Even then, this code does multiple matches and substitutions. I was hoping that it would find the first match in sample.csv, and stop further execution.
PS: I'm sure you'll notice the use of "" instead of ' ' for the code string. I am running Perl on a windows environment.
Thanks for you help!!