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