Ich versuche, einige Texte in Textdateien durch sed
zu ersetzen, weiß aber nicht, wie ich es mit mehreren Dateien machen soll.
Ich benutze:
sed -i -- 's/SOME_TEXT/SOME_TEXT_TO_REPLACE/g /path/to/file/target_text_file
Bevor ich mit den mehreren Dateien anfange, habe ich die Pfade der ausgewählten Textdateien in einer Textdatei mit dem folgenden Befehl gedruckt:
find /path/to/files/ -name "target_text_file" > /home/user/Desktop/target_files_list.txt
Jetzt möchte ich sed
gemäß target_files_list.txt
ausführen.
Sie können die Datei mit der while ... do
-Schleife durchlaufen:
$ while read i; do printf "Current line: %s\n" "$i"; done < target_files_list.txt
In Ihrem Fall sollten Sie printf ...
durch den gewünschten sed
-Befehl ersetzen.
$ while read i; do sed -i -- 's/SOME_TEXT/SOME_TEXT_TO_REPLACE/g' "$i"; done < target_files_list.txt
Beachten Sie jedoch, dass Sie nur mit find
das erreichen können, was Sie wollen:
$ find /path/to/files/ -name "target_text_file" -exec sed -i -- 's/SOME_TEXT/SOME_TEXT_TO_REPLACE/g' {} \;
Sie können mehr über die Option -exec
erfahren, indem Sie man find | less '+/-exec '
ausführen:
-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.
Wie von den Benutzern terdon und dessert in den Kommentaren korrekt angegeben, muss -r
mit read
verwendet werden, da Backslashes korrekt verarbeitet werden. Es wird auch von shellcheck
berichtet:
$ 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.
So sollte es sein:
$ while read -r i; do sed -i -- 's/SOME_TEXT/SOME_TEXT_TO_REPLACE/g' "$i"; done < target_files_list.txt
Eine Möglichkeit wäre, xargs
zu verwenden:
xargs -a target_files_list.txt -d '\n' sed -i -- 's/SOME_TEXT/TEXT_TO_REPLACE/g'
Aus 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.
Verwenden Sie einfach eine for
-Schleife.
IFS=$'\n' # Very important! Splits files on newline instead of space.
for file in $(cat files.txt); do
sed ...
done
Beachten Sie, dass Sie auf Probleme stoßen, wenn Sie Dateien mit Zeilenumbrüchen (!) Im Namen finden. (: