perlまたはsed:単語をいくつかの関係に置き換える

perlまたはsed:単語をいくつかの関係に置き換える

私はTeaCodeを使用していますが、TeaCodeではTeaCode言語テンプレートテキストには次の定義があります。

md5 converts text into MD5 hash value
uppercase makes all the letters UPPERCASE
capitalize Converts First Letter Of Each Word To Uppercase
camelcase converts text to camelCase
snakecase converts text to snake_case
dashcase converts text to dash-case
lowercase makes all the letters lowercase
sha1 converts text into SHA1 hash value
pascalcase converts text to PascalCase
remove_spaces removesallthespaces
lcfirst makes the first letter lowercase
ucfirst makes the last letter uppercase

たとえば、

For pattern vc ${name:word}, the template is:

class ${name.capitalize}ViewController: NSViewController {

    #
}

つまりvc main、ユーザーが input を入力すると、出力コードは次のようになります。

class MainViewController: NSViewController {

    |
}

だから私が望む結果は次のとおりです。

入力:テンプレート変数に置き換える単語。

出力:テンプレートテキストが置き換えられました。

例1:

テキスト入力:

class MainViewController: NSViewController {

    // this is main text
    // this is maintain
    // this is Maintain
    // this is Main text
}

入力する:

main

出力テキスト:

class ${main.capitalize}Controller: NSViewController {

    // this is ${main} text
    // this is maintain
    // this is Maintain
    // this is ${main.capitalize} text
}

注:mainはmaintainと同じ単語にのみ置き換えることができます。 mainは言葉ではありません。

ベストアンサー1

私が正しく理解したら、次のことを試すことができますsed

例:

class FooViewController: NSViewController {

    // this is foo text
    // this is foobar
    // this is Foobar
    // this is Foo text
    // foo
    // Foo
    // BarFoo
    // barfoo
}


word="foo"

注文する:

sed -e "s/^class .*Controller/class \${$word.capitalize}Controller/" -e "s/\( \|$\)$word\( \|$\)/\1\${$word}\2/" -e "s/\( \|^\)${word^}\( \|$\)/\1\${$word.capitalize}\2/" file

出力:

class ${foo.capitalize}Controller {

    // this is ${foo} text
    // this is foobar
    // this is Foobar
    // this is ${foo.capitalize} text
    // ${foo}
    // ${foo.capitalize}
    // BarFoo
    // barfoo
}

おすすめ記事