Every time this situation pops up, which is about once every few months, I end up spending way too long trying to figure out a bash script that does exactly what I want. I am pretty sure what I want is similar to what everyone else wants, but more often than not the available scripts online when I do a quick search are oversimplified and don't do these things (and my own bash scripting skills aren't good enough to figure it out):
What is the SIMPLEST way to accomplish this? I mean, I could write a script in python, it would only be a few lines, but I feel like this should be easy to accomplish in the shell.
I typically do something like this:
find . -name '*.hpp' -o -name '*.cpp' | xargs grep -l StuffToEdit
I then see if I'm getting the files of interest. If I am, I add to the pipeline:
| xargs sed -i 's@StuffToEdit@NewStuffInstead@g'
And let 'er rip.
If sed is making your VCS think files are changed when they aren't, you should look at the diffs. Perhaps you have whitespace turned off in your diffs but that's what's changing (line endings or the like)?
I'm sure someone will come and point out that one or both of my uses of xargs up there are not necessary, but for one-off jobs, it doesn't matter. I prefer these multi-program pipelines for this sort of task anyway, since I can easily disable or swap out certain filters and check the results of the rest if the results are not correct at first.
This should work as per your requirement:
find /path/to/dir/ -type f -name "*.txt" -exec sed -i 's/substitution/replacement/g' {} \;
| | | | | |
----------------- --------------------- -----------------------------------
| | |
This will do the This will allow you This will do your substitution. -i
search of files to specify filename option will do it inline. You can
recursively for You can also use use -r option to use Extended Regex
the supplied path. -regex option for as sed's native support is BRE.
better search. -type
is to limit your search.
i.e f for regular files,
d for directories, l for
symbolic links etc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With