#!/bin/bash # Search and replase by Moisey Oysgelt # Bash/Korn search and replase utilty # Craeted 10/26/2005 5:41PM version 1.0.0.1 usage() { echo 'Usage: $0 "(templates_file|expresion)" ( "file" |("search_path wild_card" )) ' exit } path=$2 wild=$3 template=$1 DT=`date +%M-%d-%y` if [ -z $1 ]; then usage exit fi if [ ! -f $template ]; then # echo "Cannot find substitue file [$template] " nofile=1 fi if [[ (! -f $path ) && (! -d $path ) ]]; then usage exit fi if [[ -d $path ]]; then if [ -z $wild ]; then usage exit fi fi if [[ $nofile -eq 1 ]]; then res=$(echo $template | grep -E s/.*/.*/) if [[ $res == "" ]]; then echo "Wring sed replase expresion. Correct has to be in format s/search/replase/" exit fi fi replase() { fname=$1 TEMP_FILE="${fname}.${DT}.tmp" BK_FILE="$fname.${DT}.bak" if [[ $nofile -eq 1 ]]; then sed $template $fname >$TEMP_FILE else sed -f $template $fname >$TEMP_FILE fi if [ $? -eq 0 ]; then diff $fname $TEMP_FILE if [ $? -eq 0 ]; then rm $TEMP_FILE echo "No replace on file [$fname ] " else mv $fname $BK_FILE mv $TEMP_FILE $fname fi else echo "Cannot execute s&r on file [$fname ] " rm $TEMP_FILE fi } if [[ -f $path ]]; then lst=$path else lst=$( find $path -name "$wild") fi for fl in $lst do replase $fl done