This really is two questions. First, you want to know how to make a modification to a string, and second, you want to know how to write to a file. (Modifying a file in place is harder, something that as a beginning Perl programmer you will probably want to leave for later.)
The second question, writing to a file, has already been addressed by the other answers. For what you're doing, you basically will loop through the input file, and write each line, either modified or not, out to the output file. (Then, if you're brave, you can move the modified output overtop of the input, but for now I would probably leave that to do manually after inspecting the output.)
However, nobody seems to have addressed the first question, making the modification to the string in the first place. You can do this with a regular expression substitution...
$_ =~ s/=.*?$/=Whatever/;
Also note that the substitution operator binds to $_ by default, so if you're operating on $_ you may leave off the $_ =~ part. (If you want to operate on the contents of any _other_ variable, though, you'd have to specify it.) Note too that since the pattern that I'm substuting for starts with =, I had to include the = in the substitution, since otherwise it would be removed. It is possible to avoid this (by excluding the = from the pattern (using a lookbehind assertion)), but the way I've written it here is easier to understand and should work just as well. The only caveat is that you do have to leave this inside the if statement that checks for your keyword (Identify in your example), but you had that already. Using the lookbehind would allow you to combine that step with the substitution:
$_ =~ s/(?<=Identity=).*?$/Whatever/;
However, there are limitations to this approach (e.g., you can't straightforwardly allow an optional space before the = sign, as the other version implicitely does), and the only tangible benefit is terseness. So on the whole I would tend to go the other way, putting the substitution inside the if block and including the = in both the pattern and the replacement string.
As for writing to the file, you can just write $_ out to the output file after the close of the if block, so that it's written whether it's been modified or not. That way the whole contents of the input file is copied over to the output file, except that it's modified if any changes are made to $_ in the if block. It's also possible to chain several elsif blocks onto the if block, thereby allowing for several possible changes. More complex variations (e.g., using a hash or array as a list of changes to make) are beyond the scope of this reply.
HTH.HAND.
In reply to Re: Modifying entries in a file.
by jonadab
in thread Modifying entries in a file.
by nisha
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |