текст из нескольких файлов заменяется на sed

Я пытаюсь заменить некоторые тексты в текстовых файлах на sed, но не знаю, как это сделать с несколькими файлами.
Я использую:

sed -i -- 's/SOME_TEXT/SOME_TEXT_TO_REPLACE/g /path/to/file/target_text_file

Прежде чем перейти к нескольким файлам, я напечатал пути целевых текстовых файлов в текстовом файле с помощью этой команды:

find /path/to/files/ -name "target_text_file" > /home/user/Desktop/target_files_list.txt

Теперь я хочу запустить sed в соответствии с target_files_list.txt.

5
задан 24 December 2017 в 14:59

3 ответа

Вы можете просмотреть файл, используя цикл while ... do:

$ while read i; do printf "Current line: %s\n" "$i"; done < target_files_list.txt

В вашем случае вы должны заменить printf ... на команду sed, которую вы хотите.

$ while read i; do sed -i -- 's/SOME_TEXT/SOME_TEXT_TO_REPLACE/g' "$i"; done < target_files_list.txt

Однако, обратите внимание, что вы можете достичь того, что вы хотите, используя только find:

$ find /path/to/files/ -name "target_text_file" -exec sed -i -- 's/SOME_TEXT/SOME_TEXT_TO_REPLACE/g' {} \;

Вы можете узнать больше о опции -exec, запустив man find | less '+/-exec ':

   -exec command ;

          Execute command; true if 0 status is returned.  All
          following arguments to find are taken to be arguments to
          the command until an argument consisting of `;' is
          encountered.  The string `{}' is replaced by the current
          file name being processed everywhere it occurs in the
          arguments to the command, not just in arguments where it
          is alone, as in some versions of find.  Both of these
          constructions might need to be escaped (with a `\') or
          quoted to protect them from expansion by the shell.  See
          the EXAMPLES section for examples of the use of the
          -exec option.  The specified command is run once for
          each matched file.  The command is executed in the
          starting directory.  There are unavoidable security
          problems surrounding use of the -exec action; you should
          use the -execdir option instead.

РЕДАКТИРОВАТЬ:

Как правильно отмечено пользователями тердон и десерт в комментариях, необходимо использовать -r с read, потому что он будет правильно обрабатывать обратную косую черту. Об этом также сообщают shellcheck:

$ cat << EOF >> do.sh
#!/usr/bin/env sh
while read i; do printf "$i\n"; done < target_files_list.txt
EOF
$ ~/.cabal/bin/shellcheck do.sh

In do.sh line 2:
while read i; do printf "\n"; done < target_files_list.txt
      ^-- SC2162: read without -r will mangle backslashes.

Так и должно быть:

$ while read -r i; do sed -i -- 's/SOME_TEXT/SOME_TEXT_TO_REPLACE/g' "$i"; done < target_files_list.txt
7
ответ дан 24 December 2017 в 14:59

Одним из способов было бы использовать xargs:

xargs -a target_files_list.txt -d '\n' sed -i -- 's/SOME_TEXT/TEXT_TO_REPLACE/g'

Из man xargs:

   -a file, --arg-file=file
          Read items from file instead of standard input.  

   --delimiter=delim, -d delim
          Input  items  are  terminated  by  the specified character.  The
          specified delimiter may be a single character, a C-style charac‐
          ter  escape  such as \n, or an octal or hexadecimal escape code.
3
ответ дан 24 December 2017 в 14:59

Просто используйте петлю for.

IFS= 

Обратите внимание, что у вас возникнут проблемы, если вы обнаружите какие-либо файлы с символами новой строки (!) В своих именах. (:

\n' # Very important! Splits files on newline instead of space. for file in $(cat files.txt); do sed ... done

Обратите внимание, что у вас возникнут проблемы, если вы обнаружите какие-либо файлы с символами новой строки (!) В своих именах. (:

2
ответ дан 24 December 2017 в 14:59

Другие вопросы по тегам:

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