4

この sed 式は、入力文字列を 2 行の出力文字列に変換します。2 つの出力行はそれぞれ、入力の部分文字列で構成されます。最初の行は大文字に変換する必要があります。

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 がそれに続くすべてのものに適用されるため、2 行目も大文字になることです。\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 に答える