Bash script to replace a string within a file

#!/bin/bash
# command line arguments are 1 search string and 2 the replacement
# handy for replacing ip addresses in host files
# example
# strreplace.sh 192.168.0.2 10.0.0.4
for f in *
do
  echo "sed -i 's/$1/$2/g' $f" #let us know what we are about to screw up
  sed -i "s/$1/$2/g $f" #making it happen
done

Leave a Reply

Your email address will not be published. Required fields are marked *