J'essaie de remplacer certains textes dans des fichiers texte par sed
mais je ne sais pas comment faire avec plusieurs fichiers.
J'utilise :
sed -i -- 's/SOME_TEXT/SOME_TEXT_TO_REPLACE/g /path/to/file/target_text_file
Avant d'aller avec les fichiers multiples, j'ai imprimé les chemins des fichiers texte ciblés dans un fichier texte avec cette commande :
find /path/to/files/ -name "target_text_file" > /home/user/Desktop/target_files_list.txt
Maintenant, je veux exécuter sed
selon target_files_list.txt
.
Réponse acceptée :
Vous pouvez parcourir le fichier en boucle en utilisant while ... do
boucle :
$ while read i; do printf "Current line: %s\n" "$i"; done < target_files_list.txt
Dans votre cas, vous devez remplacer printf ...
avec sed
commande que vous voulez.
$ while read i; do sed -i -- 's/SOME_TEXT/SOME_TEXT_TO_REPLACE/g' "$i"; done < target_files_list.txt
Cependant, notez que vous pouvez obtenir ce que vous voulez en utilisant uniquement find
:
$ find /path/to/files/ -name "target_text_file" -exec sed -i -- 's/SOME_TEXT/SOME_TEXT_TO_REPLACE/g' {} \;
Vous pouvez en savoir plus sur -exec
en exécutant 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.
MODIFIER :
Comme correctement noté par les utilisateurs terdon et dessert dans les commentaires
il faut utiliser -r
avec read
car il gèrera correctement
les barres obliques inverses. Il est également signalé par 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.
Donc ça devrait être :
$ while read -r i; do sed -i -- 's/SOME_TEXT/SOME_TEXT_TO_REPLACE/g' "$i"; done < target_files_list.txt