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!!


In reply to How to handle metacharacters in input file for Perl one-liner code by debug

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.