Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2021-10-20 07:41 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (79 votes). Check out past polls.