Posted By

symsec on 10/20/12


Tagged

regex script replace heredoc word ex exscript


Versions (?)

Bash exscript to replace word occurrences


 / Published in: Bash
 

As portrayed by the example found in the Advanced Bash Scripting guide this demonstrates a few techniques

  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  

You need to login to post a comment.