Replace inplace? #2568
-
So, I've discovered the I ended up using VS Code to perform that task which was very slow. Would it be possible to add a switch to do the replacement inplace instead of just printing it? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
The documentation for the -r flag states: "Replace every match with the text given when printing results. Neither this flag nor any other ripgrep flag will modify your files." I doubt the authors would want to modify ripgrep to do in-place editing of files. And neither should they in my opinion, as it's a file search utility and not a file editing utility. I would suggest either writing a script and/or using the find utility to iterate over your files and use sed -i to replace your text instead. |
Beta Was this translation helpful? Give feedback.
-
It's probably worth mentioning that sed -i ... will destroy symbolic links if you do an in-place replacement of text in a file that happens to be a symbolic link. The way to get around this behaviour is to use the --follow-symlinks argument i.e. sed --follow-symlinks -i... I don't know whether this might be worth adding to the ripgrep FAQ? I will of course understand if you don't feel this is within the remit of ripgrep. But you do mention sed as a possible solution to replacing text in files in the FAQ already ;-) |
Beta Was this translation helpful? Give feedback.
-
Write some script using for file in $(rg --files)
do
new=$(echo "$file" | sed -e 's/bljuz_muzika_-_//' -e 's/_(z3.fm)//' -e 's/_-_/_/g')
mv $file $new
echo "Renamed $file --> $new"
done
I tested this small script with my mp3 files about 120 files. Working properly. You can rename 50k files with this small script. That's all. |
Beta Was this translation helpful? Give feedback.
The documentation for the -r flag states: "Replace every match with the text given when printing results. Neither this flag nor any other ripgrep flag will modify your files."
I doubt the authors would want to modify ripgrep to do in-place editing of files. And neither should they in my opinion, as it's a file search utility and not a file editing utility. I would suggest either writing a script and/or using the find utility to iterate over your files and use sed -i to replace your text instead.