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

hi all

perl -i -pe 's/\.mbh_succ_roam_i_vlr_e_plmn/\.new_mbh_succ_roam_i_vlr_ +e_plmn/g' <input_file>
file content, __DATA__ .mbh_succ_roam_i_vlr_e_plmn next .mbh_succ_roam_i_vlr_e_plmn1 first .mbh_succ_roam_i_vlr_e_plmn2 verify
when I run the perl oneliner with the input file all 3 lines are getting changed. what is the change should I need to make to change only first line. In other words how can I change the code to affect only if the word matches neglecting the rest of lines.

Replies are listed 'Best First'.
Re: regex change should be applied in iputfile
by GrandFather (Saint) on Jun 01, 2007 at 04:40 UTC

    Add a word break anchor (\b) at the end of the word:

    s/\.mbh_succ_roam_i_vlr_e_plmn\b/.../g

    DWIM is Perl's answer to Gödel
Re: regex change should be applied in iputfile
by McDarren (Abbot) on Jun 01, 2007 at 04:47 UTC
    All 3 lines are being changed, because all three lines are matching. So you need to change your pattern so that only the one you want will match. The thing that is different about the first is that it has no trailing digit. So the simplest way is probably to just add a \s to the end of your matching string. So you would have this:
    perl -i -pe 's/\.mbh_succ_roam_i_vlr_e_plmn\s/\.new_mbh_succ_roam_i_vl +r_e_plmn/g' <input_file>
    Personally, I'd do it slightly differently. Perhaps like this:
    perl -i -pe 's/\.mbh([_a-z]+)\s/\.new$1/g' < input_file
    cheers,
    Darren :)
Re: regex change should be applied in iputfile
by blazar (Canon) on Jun 01, 2007 at 15:22 UTC
    when I run the perl oneliner with the input file all 3 lines are getting changed. what is the change should I need to make to change only first line. In other words how can I change the code to affect only if the word matches neglecting the rest of lines.

    Others told you what you were missing: a \b. Here, I'll give you some other WTDI (while saving some keystrokes and the risk to type something the wrong way):

    perl -pe "s/\.(mbh_succ_roam_i_vlr_e_plmn\b)/.new_$1/g" input.txt perl -pe "s/\.(?=mbh_succ_roam_i_vlr_e_plmn\b)/.new_/g" input.txt