Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case-sensitive replace of known words with sed

Tags:

regex

sed

I need to replace a set of known words with sed, but I must keep case-sensitivity of original words. For example, "Abc" is replaced with "Def", but "abc" is replaced with "def". Only the first letter of the word can vary in case (so no aBC or abC words are allowed).

I know how to do this using 2 regexps per word, but is it possible to do it using only 1 regexp per word?

like image 365
TheJavaGuy-Ivan Milosavljević Avatar asked Mar 05 '11 19:03

TheJavaGuy-Ivan Milosavljević


1 Answers

You can script the creation of the sed script. Assuming a file of word pairs:

$ cat words.dat
apple pecan
banana walnut
cherry almond

and a text file:

$ cat textfile.txt
apple
banana
cherry
I would like an apple pie. Cherry pies are good, too. What about bananas?
Bananas are full of potassium.

You can do:

awk '{print "s/" tolower($1) "/" tolower($2) "/g;s/" toupper(substr($1,1,1)) tolower(substr($1,2)) "/" toupper(substr($2,1,1)) tolower(substr($2,2)) "/g"}' words.dat > sedscript.sed

The sed script:

$ cat sedscript.sed
s/apple/pecan/g;s/Apple/Pecan/g
s/banana/walnut/g;s/Banana/Walnut/g
s/cherry/almond/g;s/Cherry/Almond/g

and then:

$ sed -f sedscript.sed textfile.txt
pecan
walnut
almond
I would like an pecan pie. Almond pies are good, too. What about walnuts?
Walnuts are full of potassium.
like image 113
Dennis Williamson Avatar answered Sep 17 '22 23:09

Dennis Williamson