frodo
frodo

Reputation: 1063

sed remove all capital letters

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'

Upvotes: 3

Views: 8921

Answers (4)

sarnold
sarnold

Reputation: 104110

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)
...

Upvotes: 1

user unknown
user unknown

Reputation: 36269

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.

Upvotes: 2

Miquel
Miquel

Reputation: 15675

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)

Upvotes: 1

Eric Fortis
Eric Fortis

Reputation: 17360

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

Upvotes: 7

Related Questions