问题
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