Quick Review: Boxee Box
December 27, 2011 – 12:22 am | 3 Comments

Some of the technical issues with Boxee Box could have been fixed if the dev team was paying more attention to addressing the bugs rather than adding “features” of dubious value. In the final analysis, for the price and ease of use, Boxee Box is the best in its class and price range. You just need to be mindful of its limitations and buy it in hope of future improvements to its usability.

Read the full story »
Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells

Useful SysAdmin Scripts: Search-and-Replace

Submitted by on June 1, 2008 – 3:03 pmNo Comment
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

Popularity: 2% [?]

Related posts:

  1. A simple text string search script
  2. Find and Replace in MySQL
  3. Generating complex SQL queries with shell scripts
  4. Wget examples and scripts
  5. Contacting the sysadmin from command line
  6. Find largest files
  7. FTP script with nested function
  8. Use Perl inside Shell scripts
  9. Database operations with SQL and Korn shell
  10. Creating table indexes in MySQL

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.