mirror of
https://github.com/jbranchaud/til
synced 2026-01-08 01:28:02 +00:00
Add Replace Pattern Across Many Files In A Project as a Unix TIL
This commit is contained in:
28
unix/replace-pattern-across-many-files-in-a-project.md
Normal file
28
unix/replace-pattern-across-many-files-in-a-project.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# 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`](https://github.com/BurntSushi/ripgrep).
|
||||
|
||||
```bash
|
||||
$ 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.
|
||||
|
||||
```bash
|
||||
$ 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`.
|
||||
Reference in New Issue
Block a user