GNU/Linux >> Znalost Linux >  >> Ubuntu

Nahradit text více souborů textem Sed?

Snažím se nahradit některé texty v textových souborech pomocí sed ale nevím, jak to udělat s více soubory.
Používám:

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

Než půjdu s více soubory, vytiskl jsem cesty k cíleným textovým souborům do textového souboru pomocí tohoto příkazu:

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

Nyní chci spustit sed podle target_files_list.txt .

Přijatá odpověď:

Soubor můžete procházet pomocí while ... do smyčka:

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

Ve vašem případě byste měli nahradit printf ... s sed příkaz, který chcete.

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

Všimněte si však, že toho, co chcete, můžete dosáhnout pouze pomocí find :

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

Můžete si přečíst více o -exec volbu spuštěním 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.

UPRAVIT:

Jak správně poznamenali uživatelé terdon a dezert v komentářích
je nutné použít -r s read protože bude správně
zpracovávat zpětná lomítka. Hlásí to také 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.

Takže by to mělo být:

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

Ubuntu
  1. Nahraďte chytré uvozovky příkazem sed pro Linux

  2. Manipulace s textem na příkazovém řádku pomocí sed

  3. Jak nahradit text podobný sedu pythonem?

  1. Použití více vzorů najednou pomocí příkazu Sed

  2. Jak nahradit soubor v jar příkazovým řádkem v linuxu?

  3. nahradit řádky v jednom souboru řádky v jiném číslem řádku

  1. Jak nahradit řetězec v souboru (souborech)?

  2. Nahradit nový řádek nulou?

  3. Příklady příkazů sed v Linuxu