Bash exscript to replace word occurrences


/ Published in: Bash
Save to your folder(s)

As portrayed by the example found in the [Advanced Bash Scripting guide](http://tldp.org/LDP/abs/html/here-docs.html) this demonstrates a few techniques


Copy this code and paste it in your HTML
  1. #!/usr/bin/env bash
  2. # Replace all instances of "Smith" with "Jones"
  3. #+ in files with a ".txt" filename suffix.
  4.  
  5. ORIGINAL=Smith
  6. REPLACEMENT=Jones
  7.  
  8. for word in $(fgrep -l $ORIGINAL *.txt)
  9. do
  10. # -------------------------------------
  11. ex $word <<EOF
  12.   :%s/$ORIGINAL/$REPLACEMENT/g
  13.   :wq
  14. EOF
  15. # :%s is the "ex" substitution command.
  16. # :wq is write-and-quit.
  17. # -------------------------------------
  18. done

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.