Is there perhaps a more elegant way to do this?
If you you input and output redirection, you can let the shell do the file handling for you.
while (<>) { s/$searchPattern1/$repalcePattern1/g; s/$searchPattern2/$replacePattern2/g; s/$searchPattern3/$replacePattern3/g; print; }
but that doesn't seem to be working
while (<>) { if (s/$searchPattern1/$repalcePattern1/g) { warn("$searchPattern1 replaced with $replacePattern1\n"); } elsif (s/$searchPattern2/$repalcePattern2/g) { warn("$searchPattern2 replaced with $replacePattern2\n"); } elsif (s/$searchPattern3/$repalcePattern3/g) { warn("$searchPattern3 replaced with $replacePattern3\n"); } else { warn("Search Patterns not found\n"); } print; }
Note that you used to search for all three patterns. Now you stop after one succeeds in replacing something.
is there a way to remove all the spaces at the end of a line and add a new line character?
s/\s*\z/\n/;
In reply to Re: Conditional Search and Replace
by ikegami
in thread Conditional Search and Replace
by PyrexKidd
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |