Ответ вawk
:
awk '/ny/ {gsub(/xy/,"ab")}; {print}' test.txt
/ny/
:выполните следующие команды, только если в строке есть ny
. gsub(/xy/,"ab")
:замените /xy/
на ab
только в этих строках. {print}
:независимо от того, на какой строке вы находитесь, вывести строку. Если я вас правильно понял, вы можете попробовать это с помощью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
}