开发者

Case-sensitive replace of known words with sed

I nee开发者_JAVA百科d 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?


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.


If you're using a list of >1000 (abc->def, ghi->jkl,...), why not turn it into a list of >2000 (abc->def, Abc->Def, ghi->jkl, Ghi->Jkl,...) in one step, (using sed 'y' or some other tool) and use that?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜