#!/bin/bash if [ $# -lt 3 ] then echo "using: $0 " exit fi if [ "$1" = "-f" ] then flag="-f" shift 1 else flag="-i" fi search="$(echo "$1"|awk '{gsub("\\","\\\\");gsub("/","\\/");print $0}')" replace="$(echo "$2"|awk '{gsub("\\","\\\\");gsub("[$]","\\$");gsub("/","\\/");print $0}')" shift 2 echo "replace \"$search\" with \"$replace\" in the files " bold=`tput smso` offbold=`tput rmso` for file in $(grep -l "$search" "$@") do tmp=/tmp/regreplace_$(basename $file) sed "s/${search}/${replace}/g" $file >$tmp echo "${bold}diffs of $file$offbold ('<' = before, '>' = after)" diff $file $tmp cp $flag $tmp $file rm -f $tmp done