Recherche et remplacement à travers beaucoup de fichiers
Afin d'atteindre une vitesse aussi proche que possible de la vitesse de l'éclair ou de la vitesse de distorsion lors de la recherche et du remplacement. à travers plusieurs fichiers (peut-être même des milliers ou des millions) dans des systèmes de fichiers massifs tels que d'énormes dépôts de code, je recommande d'utiliser Ripgrep ( rg
) qui est génial et incroyablement rapide. Malheureusement, il ne supporte pas la recherche et le remplacement dans les fichiers, et selon l'auteur ne le fera probablement jamais Nous devons donc utiliser des solutions de contournement.
Méthode 1 basée sur Ripgrep (la méthode facile, mais plus limitée)
Voici la première solution de contournement : https://github.com/BurntSushi/ripgrep/issues/74#issuecomment-376659557
Utilisez Ripgrep ( rg
) pour trouver les fichiers contenant les correspondances, puis envoyer une liste de ces fichiers à sed
pour effectuer le remplacement du texte dans ces fichiers :
# Replace `foo` with `bar` in all matching files in the current
# directory and down
rg 'foo' --files-with-matches | xargs sed -i 's|foo|bar|g'
Méthode 2 basée sur Ripgrep (mon choix recommandé)
Voici la deuxième solution de contournement : utiliser mon rgr
(Ripgrep Replace) wrapper script. J'ai écrit autour de Ripgrep qui ajoute l'option -R
pour remplacer le contenu de votre disque. L'installation est simple. Il suffit de suivre les instructions qui figurent en haut du fichier. L'utilisation est également très simple :
# Replace `foo` with `bar` in all matching files in the current
# directory and down
rgr 'foo' -R bar
Puisqu'il s'agit d'une enveloppe autour de rg
Il permet également d'accéder à toutes les autres options et fonctionnalités de Ripgrep. Voici le menu d'aide complet à ce moment, avec plus d'exemples d'utilisation :
rgr ('rgr') version 0.1.0
RipGrep Replace (rgr). This program is a wrapper around RipGrep ('rg') in order to allow some
extra features, such as find and replace in-place in files. It doesn't rely on 'sed'. It uses
RipGrep only. Since it's a wrapper around 'rg', it forwards all options to 'rg', so you can use it
as a permanent replacement for 'rg' if you like.
Currently, the only significant new option added is '-R' or '--Replace', which is the same as
RipGrep's '-r' except it MODIFIES THE FILES IN-PLACE ON YOUR FILESYSTEM! This is great! If you
think so too, go and star this project (link below).
USAGE (exact same as 'rg')
rgr [options] <regex> [paths...]
OPTIONS
ALL OPTIONS ACCEPTED BY RIPGREP ('rg') ARE ALSO ACCEPTED BY THIS PROGRAM. Here are just a few
of the options I've amended and/or would like to highlight. Not all Ripgrep options have been
tested, and not all of them ever will be by me at least.
-h, -?, --help
Print help menu
-v, --version
Print version information.
--run_tests
Run unit tests (none yet).
-d, --debug
Turn on debug prints. '-d' is not part of 'rg' but if you use either of these options here
it will auto-forward '--debug' to 'rg' under-the-hood.
-r <replacement_text>, --replace <replacement_text>
Do a dry-run to replace all matches of regular expression 'regex' with 'replacement_text'.
This only does the replacement in the stdout output; it does NOT modify your disk!
-R <replacement_text>, --Replace <replacement_text>
THIS IS THE ONE! Bingo! This is the sole purpose for the creation of this wrapper. This
option will actually replace all matches of regular expression 'regex' with
'replacement_text' ON YOUR DISK. It actually modifies your file system! This is great
for large code-wide replacements when programming in large repos, for instance.
--stats
Show detailed statistics about the ripgrep search and replacements made.
SEE ALSO 'rg -h' AND 'man rg' FOR THE FULL HELP MENU OF RIPGREP ITSELF, WHICH OPTIONS ARE ALSO
ALLOWED AND PASSED THROUGH BY THIS 'rgr' WRAPPER PROGRAM.
EXAMPLE USAGES:
rgr foo -r boo
Do a *dry run* to replace all instances of 'foo' with 'boo' in this folder and down.
rgr foo -R boo
ACTUALLY REPLACE ON YOUR DISK all instances of 'foo' with 'boo' in this folder and down.
rgr foo -R boo file1.c file2.c file3.c
Same as above, but only in these 3 files.
rgr foo -R boo -g '*.txt'
Use a glob filter to replace on your disk all instances of 'foo' with 'boo' in .txt files
ONLY, inside this folder and down. Learn more about RipGrep's glob feature here:
https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md#manual-filtering-globs
rgr foo -R boo --stats
Replace on your disk all instances of 'foo' with 'boo', showing detailed statistics.
Note to self: the only free lowercase letters not yet used by 'rg' as of 3 Jan. 2021 are:
-d, -k, -y
This program is part of eRCaGuy_dotfiles: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles
by Gabriel Staples.
Installation de Ripgrep
Pour Ubuntu 20.04 ou plus :
sudo apt update && sudo apt install ripgrep
Pour les versions antérieures d'Ubuntu, suivez les instructions de Debian ici : https://github.com/BurntSushi/ripgrep#installation