4

此 sed 表达式将输入字符串转换为两行输出字符串。两条输出线中的每一条都由来自输入的子字符串组成。第一行需要转换为大写:

s:random_stuff\(choice1\|choice2\){\([^}]*\)}:\U\1\n\2:

目的是转换

random_stuff_choice1{This is a sentence that MAY contain AnyThing}
random_stuff_choice2{This is another similar sentence}

进入

CHOICE1
This is a sentence that MAY contain AnyThing
CHOICE2
This is another similar sentence

我遇到的问题是 \U 适用于它后面的所有内容,因此第二行也是大写的。是否可以使 \U 仅适用于第一场比赛?

4

2 回答 2

4

用于\E取消\U

s:random_stuff_\(choice1\|choice2\){\([^}]*\)}:\U\1\E\n\2:
于 2013-02-28T13:44:10.843 回答
4

sed

$ sed 's/.*\(choice[0-9]\+\){\([^}]*\)}/\U\1\n\E\2/' file
CHOICE1
This is a sentence that MAY contain AnyThing
CHOICE2
This is another similar sentence

awk

$ awk -F'{|}' 'gsub(/.*_/,""){print toupper($1)"\n"$2}' file
CHOICE1
This is a sentence that MAY contain AnyThing
CHOICE2
This is another similar sentence
于 2013-02-28T13:54:19.063 回答