简体   繁体   English

如何使用sed将引号内的所有字母大写?

[英]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? 有人可以向我解释如何使用sed将引号内的所有字母大写? For example "abcd1234" should become "ABCD1234". 例如,“abcd1234”应变为“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的版本,但在我使用的那个(GNU sed版本4.1.5)中,你可以编写s/"[^"]*"/\\U\\0/ 。例如,这个Bash命令:

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. 除了\\U之外没有太多要解释的,这是一个特殊功能,可以使替换字符串中的后续文本大写,最多为\\E或字符串结尾。

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. 上面命令(。 )的小解释将匹配所有字符,所以使用转义序列它将是(。 )\\ U - 要更改为大写..假设你想要改变每个小写使用\\ L然后\\ 1粘贴第一个存储的值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM