perl или sed: заменить слова некоторым отношением

Ответ вawk:

awk '/ny/ {gsub(/xy/,"ab")}; {print}' test.txt

Пояснение

  • /ny/:выполните следующие команды, только если в строке есть ny.
  • gsub(/xy/,"ab"):замените /xy/на abтолько в этих строках.
  • {print}:независимо от того, на какой строке вы находитесь, вывести строку.
0
04.05.2020, 02:39
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
}
0
28.04.2021, 23:16

Теги

Похожие вопросы