Useful SysAdmin Scripts: Search-and-Replace
Let’s say you moved your server to a new network and now it has a different IP address. You know your users have a habit of hard-coding the server’s IP into their scripts. So you want to scan a bunch of directories and replace the old IP with the new one in all text files. The script below will do just that. It allows you to specify search path, filename pattern, search pattern, and replacement pattern. The script will also create backups of all files it modifies.
Download: find_replace.sh ; or find_replace.ksh
#!/bin/sh
#---------------------------------------------
# FUNCTIONS
#---------------------------------------------
enter_dir_func() {
SDIR=
CDIR=`pwd`
echo ""
echo -n "Enter directory to search [$CDIR]: "
read SDIR
if [ ! -n "$SDIR" ]
then
SDIR=$CDIR
else
if [ ! -d $SDIR ]
then
echo "Directory $SDIR does not exist!"
echo 1
fi
fi
}
#---------------------------------------------
enter_find_string_func() {
FINDSTR=
echo -n "Enter search string: "
read FINDSTR
if [ ! -n "$FINDSTR" ]
then
echo "Search string cannot be null!"
exit 1
fi
}
#---------------------------------------------
enter_replace_string_func() {
REPLSTR=
echo -n "Enter replace string: "
read REPLSTR
}
#---------------------------------------------
enter_filename_string_func() {
FILESTR=
echo -n "Enter filename string: "
read FILESTR
}
#---------------------------------------------
search_replace_func() {
echo "Performing find-and-replace..."
DATE=`date +'%Y%m%d%H%M%S'`
find $SDIR -type f -name "*${FILESTR}*" | while read FILENAME
do
if [ `file "$FILENAME" | awk '{print $NF}'` == "text" ]
then
if [ `grep -c "$FINDSTR" "$FILENAME"` -gt 0 ]
then
echo "-----------------------------------------------"
echo "Replacing in $FILENAME"
cp -p "$FILENAME" "${FILENAME}_${DATE}_BACKUP"
gzip "${FILENAME}_${DATE}_BACKUP"
sed "s+${FINDSTR}+${REPLSTR}+g" "$FILENAME" > "${FILENAME}_${DATE}"
mv -f "${FILENAME}_${DATE}" "$FILENAME"
echo "Backup file saved as ${FILENAME}_${DATE}.gz"
fi
fi
done
}
#---------------------------------------------
confirm_func() {
clear
RESP=
echo "Will search $SDIR for filenames mathing '*${FILESTR}*' pattern."
echo "And will replace all occurences of '${FINDSTR}' with '${REPLSTR}'."
echo -n "Do you wish to continue? (y/n): "
read RESP
case $RESP in
[yY] | [yY][eE][sS] ) search_replace_func ;;
* ) exit 0
esac
}
#---------------------------------------------
# RUNTIME
#---------------------------------------------
enter_dir_func
enter_filename_string_func
enter_find_string_func
enter_replace_string_func
confirm_func


Today he kept looking down, showing off his phone and pretending to look near my side really quick then he seems like he can’t make any contact with me close up but far away he does… i dont know why but last time we both landed on to each others eyes 3-4 secs it was awk yeah i do like him, but im pretty sure he does too but i felt so ignored these days by him but same time i feel stared at.
My girlfriend and I decided to take a break two days ago. We see each other every day at school but we never talk now. I wanted to talk so I went up to her but she ended the conversation really short and now its just awk. Does break mean “pretend that I’m not here”?