bash sed 删除所有大写字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8424213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sed remove all capital letters
提问by frodo
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 ?
我正在尝试使用下面的 sed 命令仅在以下字符串中删除所有出现的大写字母,但它仅输出我输入的字符串 - 如何正确放置替换?
echo "Dog boy Did Good" | sed 's/\([A-Z]\+\)//g'
回答by Eric Fortis
echo "Dog boy Did Good" | sed 's/[A-Z]//g'
回答by user unknown
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.
你用空替换某些东西(大写),你不需要对它进行分组,因为你以后不使用它,你不需要+,因为最后的 g 执行全局替换。
回答by sarnold
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.
您现在的答案很好,假设您的所有大写字母都通过 表示[A-Z],这是常规美国英语中的标准,但没有通过土耳其测试,该测试有多个字母 变体i。
Better would be to use the [[:upper:]]mechanism, which will respect the current locale(7):
更好的是使用该[[:upper:]]机制,该机制将尊重当前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(1)命令可以轻松删除:
$ tr -d [[:upper:]] < /etc/motd
elcome to buntu 11.04 (/inux 2.6.38-12-generic x86_64)
...
回答by Miquel
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)
如果您想完全删除它们,请不要在 sed 表达式的后半部分使用 \1,因为它会添加到第一个匹配项中(这就是您要替换的内容)

