Question

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

Was it helpful?

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.

OTHER TIPS

echo abcd1234 | sed 's/\(.*\)/\U\1/'

small explanation to above command (.) will match all the characters so with escape sequence it will be (.) \U --To change to upper case.. suppose you want to change every thing to lower case use \L then \1 to paste first stored value.

Source: http://nixcraft.com/shell-scripting/15862-sed-convert-text-lower-upper-case.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top