1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00
Files
til/unix/replace-pattern-across-many-files-in-a-project.md

1000 B

Replace Pattern Across Many Files In A Project

Let's say I have a bunch of files in the db/migrate directory of my project that contain a line of text I'd like to update. I want to replace all occurrences of t.bigint with t.integer.

First, I can get an idea of the scope of the change by listing out all the files and lines where this pattern exists using ripgrep.

$ rg 't.bigint' db/migrate

I can visually do a quick scan to make sure I'm not picking up results with my pattern that weren't intended.

Then, I can include the --files-with-matches flag to limit the ripgrep output to just filenames. Those filenames can be piped to sed (via xargs) which will do the pattern replacement.

$ rg --files-with-matches 't.bigint' db/migrate | \
  xargs sed -i '' 's/t.bigint/t.integer/g'

This does an in-place (-i '') edit of the files substituting (s///g) globally the occurrences of t.bigint for t.integer.