leelu has asked for the wisdom of the Perl Monks concerning the following question:

Please need some help how to replace only "text" with "update" Eg: i have data as below and first record "text" need to change to "update" and not other records input file ------------ text -> only this record need to replace text name text city text address output file ------------- update text name text city text address I have tried with s/text/update/g; all records are updated instead of first record
  • Comment on Replace specific matching word with differnt word

Replies are listed 'Best First'.
Re: Replace specific matching word with differnt word
by AnomalousMonk (Archbishop) on May 28, 2015 at 03:55 UTC

    leelu: In addition to roboticus's comment about not showing any code, please note that you're not really showing any data either — at least not in a form that can easily be interpreted. Please Update your original post to use  <c> ... </c> or  <code> ... </code> tags around code and data, (and input/output, if any). Please see Markup in the Monastery and Writeup Formatting Tips.


    Give a man a fish:  <%-(-(-(-<

Re: Replace specific matching word with differnt word
by roboticus (Chancellor) on May 28, 2015 at 03:45 UTC

    leelu:

    If all records are updated, then read only one record at a time, and perform your substitution only on the first record. You don't show code, so I can't tell you exactly what you're doing wrong. (You could be reading in the entire file and doing the substitution globally, you could be substituting in a loop. Hard to say without seeing the code.)

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Replace specific matching word with differnt word
by Anonymous Monk on May 28, 2015 at 06:20 UTC

    I have tried with s/text/update/g; all records are updated instead of first record

    Why do you think this happened, what do you think needs to change to fix it?

    How is the line record that needs to change substitution different from all the other records, what uniquely identifies that one record?

    Think about it, say it out loud, write down the answer and share with us

    Then visit http://perldoc.perl.org/perlintro.html#More-complex-regular-expressions