Bash: any command to replace strings in text files?

I have a hierarchy of directories containing many text files. I would like to search for a specific text line every time it appears in one of the files, and replace it with another line. For example, I can replace each occurrence of the string "Coke" with "Pepsi". Does anyone know how to do this? I am wondering if there is some kind of Bash command that can do this without loading all of these files into the editor or without creating a more complex script to do this.

I found this page explaining the trick with sed, but it does not work on files in subdirectories.

+3
source share
7 answers

Use sedin conjunction with find. For instance:

find . -name "*.txt" | xargs sed -i s/Coke/Pepsi/g

or

find . -name "*.txt" -exec sed -i s/Coke/Pepsi/g {} \;

(see search man page for more information)

+15
source

IMO, the tool with the easiest use for this task is rpl:

rpl -R Coke Pepsi .

( -Rfor recursive replacement in all subdirectories)

+3
source

sed find :

find . -name "file.*" -exec sed -i 's/Coke/Pepsi/g' {} \;
+3

find . -type f -exec sed -i 's/old-word/new-word/g' {} \;

+3

perl. - , , :

% perl -pi -e 's/Coke/Pepsi/g;' $filename

EDIT

% find ./ -exec perl -pi -e 's/Coke/Pepsi/g;' {} \;
+2

find sed

+2

Source: https://habr.com/ru/post/1746051/


All Articles