Change case preserving the same case

how can i do a case insensitive search/replace but keep the same case?

e.g., i want to change a word like

apple-pie to orange-cake

but for the first word if the first letter of the keyword or the letter after the - is capitalised i want to preserve that

e.g., if the before is:

Apple-pie

output should be:
Orange-cake

If the before is: Apple-Pie
output should be Orange-Cake

thanks

Just need a little more info... what shell-tool or programming language are you intending to use for this?

1 Like

bash
thanks :slight_smile:

It would help if you can provide a sample input, say about 10-20 lines from the original file (after blotting sensitive data) and desired output.

E.g.,

Before After
--------------
emily-yellow.extension emily-yellow.anotherword.extension/possiblysomethingelse
Emily-yellow.extension Emily-yellow.anotherword.extension/possiblysomethingelse
emily-Yellow.extension emily-Yellow.anotherword.extension/possiblysomethingelse
Emily-Yellow.extension Emily-Yellow.anotherword.extension/possiblysomethingelse


Basically I want to preserve the case in the words
At least:
-the first letter in the entire keyword
-first letter after the -
-first letter of the extension

right now i'm writing the second keyword in lowercase in the see command but i want to keep it as the same case as the original. sometimes occurrences are the beginning of a sentence, some occurrences are not. if it is, i want to also be capitalised upon replacing

---------- Post updated at 12:23 AM ---------- Previous update was at 12:17 AM ----------

actually i have another problem

I'm doing something like

UPDATE $optionstable SET option_value = replace(option_value,"$BEFORE","$AFTER") WHERE wp_otpions.option_name = 'blogname';

but the problem is that this only replaces the specific case of "$BEFORE"

how can i make it case insensitive?
thanks

---------- Post updated at 12:51 AM ---------- Previous update was at 12:23 AM ----------

Okay, so this is what I'm trying now

blognamebefore = (i put in a sql command to grab the blog name)
This can be anything like "afdsdsfasdf BEFORE fafdsadffdsds"

then I'm trying to run a sed command that does a replace of the BEFORE keyword to the AFTER keyword

sed 's!${BEFORE}!${AFTER}!gI'

I want to preserve the case of the BEFORE word, at least the first letter would be good

how can I do this?

when I run the sed command, it changes the keyword into all lowercase letters

and I don't want to do this especially in the title

---------- Post updated at 12:55 AM ---------- Previous update was at 12:51 AM ----------

I think I managed something for now
I googled about how to use sed to capitalise the first letter of a word
then use that as my new keyword

Your examples are unclear. It seems to me you asked for the ability to convert all characters to lowercase, except the first letter of each "word" is preserved. But your examples show the trivial case.

It seems to me this problem is too complex for sed/awk (without hard-coding the before/after values for each case). I think perl has an elegant solution:

perl -pe 's/(?<![a-zA-Z0-9])([a-zA-Z])([a-zA-Z0-9]*)/$1\L$2\E/g'

Before:

.TesT thIs-ThiNg Now

After:

.Test this-Thing Now

If you want to add your extension-appending code, it's pretty trivial to do that in perl. First, make sure this code will work for you.