Search code examples
regexlinuxunixsedcapitalize

How to capitalize all letters inside quotation marks using sed?


Can someone please explain to me how I can capitalize all the letters inside quotation marks using sed? For example "abcd1234" should become "ABCD1234". The length of the text inside the quotation marks is variable. I'm looking for an explanation not just the answer.

EDIT: I'm going to be using this on a text file. Suppose I have a text file like this:

Text that I don't want to capitalize and also "text that I want to capitalize" And some other text and "some occasional quoted text and maybe numbers" ...


Solution

  • This may depend on the version of sed, but in the one I have handy (GNU sed version 4.1.5), you can write s/"[^"]*"/\U\0/. For example, this Bash command:

    sed 's/"[^"]*"/\U\0/g' <<< 'foo "bar" baz'
    

    prints this:

    foo "BAR" baz
    

    There's not really much to explain except the \U, which is a special feature that causes the subsequent text in the replacement-string to be capitalized, up to either \E or end-of-string.