I came up with this (I'm sure there are much shorter, easier ways to do it, hence this post).Given the following file: *** begin file *** old_word OLD_WORD oLd_wOrd olD_worD *** end file *** Is there a substitute command that will transform the file to: *** begin substituted file *** new_word NEW_WORD nEw_wOrd neW_worD *** end substituted file *** That is to say, the replacement text will presereve the original.
#!/usr/bin/perl @words = qw[ old_word OLD_WORD old_WORD oLd_WoRd OLD_word ]; foreach $word ( @words ) { $new = 'this is the new string'; print "$word:$new:".UpperLower( $word, $new )."\n"; } sub UpperLower { my ( $old, $new ) = @_; my ( $a ); ( length( $old ) >= length( $new ) ) ? $length = length( $old ) : $ +length = length( $new ); for ( $i = 1; $i < $length+1; $i++ ) { if ( uc( substr( $old, $i-1, 1 ) ) eq substr( $old, $i-1, 1 ) && substr( $old, $i-1, 1 ) ) { $a .= uc( substr( $new, $i-1, 1 ) ); } elsif ( uc( substr( $old, $i-1, 1 ) ) ne substr( $old, $i-1, 1 ) && substr( $old, $i-1, 1 ) ) { $a .= lc( substr( $new, $i-1, 1 ) ); } else { $a .= substr( $new, $i-1, 1 ); } } return $a; }
In reply to Substitute Question by RiotTown
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |