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

Просто нажмите эту ссылку. Он автоматически установит кодеки.

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

9 ответов

Вы можете прокручивать файл с помощью цикла 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.

EDIT:

Как правильно отмечено пользователями terdon и десертом в комментариях, необходимо использовать -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
ответ дан 22 May 2018 в 15:56
  • 1
    read не увидит последнюю строку входного файла, если она не заканчивается символом новой строки, вместо этого я рекомендую while IFS='' read -r i || [[ -n "$i" ]]; do …. – dessert 24 December 2017 в 15:57
  • 2
    Я ценю ваше замечание. Однако обратите внимание, что пользователь использует >, который будет обрабатывать новые строки автоматически и что в мире unix распространено общее количество файлов с помощью newilne: stackoverflow.com/questions/729692/… . – Arkadiusz Drabczyk 24 December 2017 в 16:00
  • 3
    Вы совершенно правы, и так каждый должен это сделать, но я предпочитаю адаптировать код к пользователю, а не наоборот. ;) – dessert 24 December 2017 в 16:06
  • 4
    @ArkadiuszDrabczyk проблема заключается не столько в редком появлении файла без терминальной новой строки, а в гораздо более распространенной проблеме имен файлов с пробелами. Использование while IFS= делает эту работу над именами файлов с пробелами (или вкладками, символами новой строки и т. Д.) И с помощью -r заставляет его работать с именами файлов с обратными косыми чертами. Ваше решение find может иметь дело с произвольными именами файлов, но для подхода while требуются изменения десерта, которые предлагают сделать его надежным. – terdon♦ 24 December 2017 в 16:29
  • 5
    @ArkadiuszDrabczyk - единственный случай, когда я знаю, где он имеет значение (по крайней мере, в текущих версиях bash), если имя файла запускается с пробелом – steeldriver 24 December 2017 в 17:28

Вы можете прокручивать файл с помощью цикла 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.

EDIT:

Как правильно отмечено пользователями terdon и десертом в комментариях, необходимо использовать -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
ответ дан 18 July 2018 в 00:32

Вы можете прокручивать файл с помощью цикла 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.

EDIT:

Как правильно отмечено пользователями terdon и десертом в комментариях, необходимо использовать -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 July 2018 в 17:14

Один из способов - использовать 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
ответ дан 22 May 2018 в 15:56

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

IFS=$'\n' # Very important! Splits files on newline instead of space.

for file in $(cat files.txt); do
    sed ...
done

Обратите внимание, что в ваших именах вы столкнетесь с проблемами, если вы столкнетесь с файлами с символами новой строки (!). (:

2
ответ дан 22 May 2018 в 15:56

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

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

Обратите внимание, что в ваших именах вы столкнетесь с проблемами, если вы столкнетесь с файлами с символами новой строки (!). (:

2
ответ дан 18 July 2018 в 00:32

Один из способов - использовать 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
ответ дан 18 July 2018 в 00:32

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

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

Обратите внимание, что в ваших именах вы столкнетесь с проблемами, если вы столкнетесь с файлами с символами новой строки (!). (:

2
ответ дан 24 July 2018 в 17:14

Один из способов - использовать 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 July 2018 в 17:14

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

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