Replace a String in Multiple Files

by Mareeswari 2013-04-30 16:11:45

SED command to Replace String in Multiple Files:

SED command is used to replace a text string with a new string in multiple files.

file.sh

#!/bin/bash
OLD="echo 'hai'"
NEW="'hai'"
DPATH="/opt/lampp/htdocs/test/*.php"
BPATH="/opt/lampp/htdocs/test1/"
[ ! -d $BPATH ] && mkdir -p $BPATH || :
for f in $DPATH
do
#if [ -f $f -a -r $f ]; then
# /bin/cp -f $f $BPATH
# sed "s/$OLD/$NEW/g" "$f" > $TFILE && mv $TFILE "$f"
#else
# echo "Error: Cannot read $f"
#fi
echo $f
fname=`/usr/bin/basename $f`
echo $fname
sed "s/$OLD/$NEW/g" "$f" > "$BPATH""$fname"
done
#/bin/rm $TFILE
791
like
0
dislike
0
mail
flag

You must LOGIN to add comments