GNU/Linux >> Linux Esercitazione >  >> Ubuntu

Sostituisci più file di testo con Sed?

Provo a sostituire alcuni testi nei file di testo con sed ma non so come farlo con più file.
Io uso:

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

Prima di andare con i file multipli ho stampato i percorsi dei file di testo di destinazione in un file di testo con questo comando:

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

Ora voglio eseguire sed secondo target_files_list.txt .

Risposta accettata:

Puoi scorrere il file usando while ... do ciclo:

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

Nel tuo caso dovresti sostituire printf ... con sed comando che vuoi.

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

Tuttavia, nota che puoi ottenere ciò che desideri utilizzando solo find :

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

Puoi leggere di più su -exec opzione eseguendo 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.

MODIFICA:

Come correttamente notato dagli utenti terdon e dessert nei commenti
è necessario utilizzare -r con read perché gestirà correttamente
le barre inverse. Viene anche segnalato da 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.

Quindi dovrebbe essere:

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

Ubuntu
  1. Sostituisci le virgolette intelligenti con il comando sed di Linux

  2. Manipolazione del testo da riga di comando con sed

  3. Utilizzo di più modelli contemporaneamente con il comando Sed

  4. Come sostituire una stringa in un file?

  5. Come utilizzare Sed o Ex per sostituire un blocco (codice multilinea) con un nuovo blocco di testo (codice)?

Comando Linux WC con esempi

Come leggere un file con spazi in Linux

Manipolazione del testo con sed e grep

Come copiare un file con un comando in più directory su Linux

Converti testo in voce con eSpeak su Ubuntu

È possibile con Gedit o la riga di comando modificare ogni quarta riga di un file di testo?