Удалить строку из всех файлов, содержащих эту строку

Вместо того, чтобы пытаться модифицировать фиктивный RA для выполнения произвольных сценариев, вы могли бы рассмотреть возможность использования anythingагента ресурсов -.

# pcs resource describe ocf:heartbeat:anything
ocf:heartbeat:anything - Manages an arbitrary service

This is a generic OCF RA to manage almost anything.

Resource options:
  binfile (required): The full name of the binary to be executed.
                      This is expected to keep running with the
                      same pid and not just do something and
                      exit.
  cmdline_options: Command line options to pass to the binary
  workdir: The path from where the binfile will be executed.
  pidfile: File to read/write the PID from/to.
  logfile: File to write STDOUT to
  errlogfile: File to write STDERR to
  user: User to run the command as
  monitor_hook: Command to run in monitor operation
  stop_timeout: In the stop operation: Seconds to wait for kill
                -TERM to succeed before sending kill -SIGKILL.
                Defaults to 2/3 of the stop operation timeout.

Вы должны указать агенту anythingсвой сценарий в качестве параметра binfile=, затем, если у вас есть какой-либо способ мониторинга вашего пользовательского приложения, кроме проверки работающего pid (, это то, что агент anythingпо умолчанию ), это можно определить в параметре monitor_hook.

-3
16.02.2017, 09:22
1 ответ
for file in $(find . -mindepth 1 -type f); do sed -i 's/remove\ this\ sentence/stringtoreplace/g' $file; done

Давайте разберемся.

$ (find. -Mindepth 1 -type f)

Это найдет каждый файл, начинающийся в рабочем каталоге, и вернет его в виде полного пути. Рекурсивный в том смысле, что вы выполняете поиск на глубине не менее одного (того же каталога) или нескольких подкаталогов.

sed -i 's / remove \ this \ предложение / stringtoreplace / g'

-i modify file in-place
/g all occurrences of "remove this sentence" in lines as they're read.

И еще есть цикл for, который выполняет итерацию по файлам.

Наверное, проще

Это можно сделать еще одним способом:

find . -mindepth 1 -type f -exec sed -i 's/remove\ this\ sentence/stringtoreplace/g' {} \;

... что то же самое. Здесь мы просто используем find для поиска файлов и выполняем команду sed replace in-place для каждого найденного файла.

2
28.01.2020, 05:19

Теги

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