将某些匹配转换为大写的Sed表达式

时间:2013-02-28 13:42:52

标签: sed

此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仅适用于第一场比赛?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

使用\E取消\U

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