Deb wrote: > Hi, > > I am modifying a file and replacing a string that I find, which works > just fine. However, sometimes there is already a string there that I > don't want to replace, but instead append something to it. > > Here's what I've got so far: (obligatory use statements not included > here) > > while (<IN>) { > > s/^This_Text.*$/That_Text = 2/; > > } > > But now, I may already have "That_Text = 2" which I don't want to > replace, but instead append ":New_Text = 4". > > What would be a good method for post-appending text? What about > something like this? (Seems kind cumbersum.) > > my $addText = ":New_Text = 4"; > > while (<IN>) { > if ( $_ =~ /^This_Text/) { > my $text =~ /^*$/; > my newText = $text . $addText > } > > Recommendations?
Two options. If you especially want to stick with the substitution: s/^(This_Text.*)$/$1:New_Text = 4/; but a people will be at my throat if I recommend that, so $_ .= ':New_Text = 4' if /^This_Text/; which is the same as your solution above but more compact (except that this version modifies $_ as the substitution does, whereas yours delivers the result in $newText). HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]