sed remove all capital letters

混江龙づ霸主 提交于 2021-02-16 13:54:12

问题


I am trying to delete all occurences of Capital Letters only in the following string with the sed command below but it is only outputting the sting that I enter - how do I put the substitution in correctly ?

echo "Dog boy Did Good" | sed 's/\([A-Z]\+\)/\1/g'

回答1:


echo "Dog boy Did Good" | sed 's/[A-Z]//g'



回答2:


echo "Dog boy Did Good" | sed 's/[A-Z]//g'
og boy id ood

You substitute something (UPPERCASE) with nothing, and you don't need to group it, because you don't use it later, and you don't need +, because the g in the end performs the substitution globally.




回答3:


If you want to remove them completely, don't use \1 in the second half of the sed expression, since that adds in the first match (which is what you're trying to replace)




回答4:


The answers you have now are good, assuming all your upper case letters are represented via [A-Z], as is standard in regular American English, but fails the Turkey test, which has several variants of the letter i.

Better would be to use the [[:upper:]] mechanism, which will respect the current locale(7):

$ sed 's/[[:upper:]]//g' /etc/motd
elcome to buntu 11.04 (/inux 2.6.38-12-generic x86_64)
...

Another alternative that I want to mention; the tr(1) command can do deletions easily:

$ tr -d [[:upper:]] < /etc/motd 
elcome to buntu 11.04 (/inux 2.6.38-12-generic x86_64)
...


来源:https://stackoverflow.com/questions/8424213/sed-remove-all-capital-letters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!