문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top