Return to Snippet

Revision: 60109
at October 20, 2012 17:51 by symsec


Initial Code
#!/usr/bin/env bash
#  Replace all instances of "Smith" with "Jones"
#+ in files with a ".txt" filename suffix. 

ORIGINAL=Smith
REPLACEMENT=Jones

for word in $(fgrep -l $ORIGINAL *.txt)
do
  # -------------------------------------
  ex $word <<EOF
  :%s/$ORIGINAL/$REPLACEMENT/g
  :wq
EOF
  # :%s is the "ex" substitution command.
  # :wq is write-and-quit.
  # -------------------------------------
done

Initial URL


Initial Description
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

Initial Title
Bash exscript to replace word occurrences

Initial Tags
regex, script, replace

Initial Language
Bash