How to Replace Text while Maintaining Capitalization?

Viewed 292

Say I wanted to a string replace on a file with the following contents

name
nAmE
naMEbb
NAME

And wanted to replace the word "name" with "dave", but keeping the capitalization of the original text. For example, my desired output would be,

dave
dAvE
daVEbb
DAVE

Are there any one-liners to do this (preferably in Perl so I can do an in-place substitution across many files)?

EDIT The problem is ambiguous unless both strings have exactly the same length. Let's assume it does.

3 Answers
Related