Skip to main content
added 294 characters in body
Source Link
Gabriel Staples
  • 3.1k
  • 3
  • 34
  • 52

Update July 2022: I have a really robust wrapper tool, rgr, which stands for "RipGrep Replace" and which wraps the incredibly fast RipGrep tool (rg), which you should use instead. See my other answer here. My wrapper supports all rg options, while adding -R for actual, on-disk text replacements.


Here I use grep to tell if it is going to change a file (so I can count the number of lines changed, and replacements made, to output at the end), then I use sed to actually change the file. Notice the single line of sed usage at the very end of the Bash function below:

Here I use grep to tell if it is going to change a file (so I can count the number of lines changed, and replacements made, to output at the end), then I use sed to actually change the file. Notice the single line of sed usage at the very end of the Bash function below:

Update July 2022: I have a really robust wrapper tool, rgr, which stands for "RipGrep Replace" and which wraps the incredibly fast RipGrep tool (rg), which you should use instead. See my other answer here. My wrapper supports all rg options, while adding -R for actual, on-disk text replacements.


Here I use grep to tell if it is going to change a file (so I can count the number of lines changed, and replacements made, to output at the end), then I use sed to actually change the file. Notice the single line of sed usage at the very end of the Bash function below:

added 361 characters in body
Source Link
Gabriel Staples
  • 3.1k
  • 3
  • 34
  • 52

Update: the below code has been upgraded and is now part of my eRCaGuy_dotfiles project as "find_and_replace.sh" here. <-- I recommend you use this tool now instead.

Usage:

Usage:

Update: the below code has been upgraded and is now part of my eRCaGuy_dotfiles project as "find_and_replace.sh" here. <-- I recommend you use this tool now instead.

Usage:

deleted 22 characters in body
Source Link
Gabriel Staples
  • 3.1k
  • 3
  • 34
  • 52
$ gs_replace_str "do" "bo" test_folder/test2.txt 

9 matches found on 6 lines in file "test_folder/test2.txt":
1:hey how are you doing today
2:hey how are you doing today
3:hey how are you doing today
4:hey how are you doing today  hey how are you doing today  hey how are you doing today  hey how are you doing today
5:hey how are you doing today
6:hey how are you doing today?
$SHLVL:3 
$ gs_replace_str "do" "bo" test_folder/test2.txt 

9 matches found on 6 lines in file "test_folder/test2.txt":
1:hey how are you doing today
2:hey how are you doing today
3:hey how are you doing today
4:hey how are you doing today  hey how are you doing today  hey how are you doing today  hey how are you doing today
5:hey how are you doing today
6:hey how are you doing today?
$SHLVL:3 

Screenshot of the output, to show the matched text being highlighted in red:

$ gs_replace_str "do" "bo" test_folder/test2.txt 

9 matches found on 6 lines in file "test_folder/test2.txt":
1:hey how are you doing today
2:hey how are you doing today
3:hey how are you doing today
4:hey how are you doing today  hey how are you doing today  hey how are you doing today  hey how are you doing today
5:hey how are you doing today
6:hey how are you doing today?
$SHLVL:3 

Screenshot of the output:

$ gs_replace_str "do" "bo" test_folder/test2.txt 

9 matches found on 6 lines in file "test_folder/test2.txt":
1:hey how are you doing today
2:hey how are you doing today
3:hey how are you doing today
4:hey how are you doing today  hey how are you doing today  hey how are you doing today  hey how are you doing today
5:hey how are you doing today
6:hey how are you doing today?
$SHLVL:3 

Screenshot of the output, to show the matched text being highlighted in red:

added 106 characters in body
Source Link
Gabriel Staples
  • 3.1k
  • 3
  • 34
  • 52
Loading
Source Link
Gabriel Staples
  • 3.1k
  • 3
  • 34
  • 52
Loading