Bulk text replaces + shell script to change the file name

Bash Language : batch text replace + shell script to change the file name
#! / Bin / bash

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Name: repren.sh
# Description:
# make the following order things:
... # 1 to find specific types of files, such as h, cpp
. # 2 The contents of the file text OLD_TEXT replaced New_text
. # 3 of the file name containing OLD_TEXT rename New_text
. # 4 of containing OLD_TEXT Rename the directory name New_text
#
# Author: Breaker <breaker.zy_AT_gmail>
# Date: 2011-10
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

# IFS said for the statement separator between
OLDIFS = $ IFS
IFS = $ '\ N'    # $ to start making literal escape, otherwise \ n literal rather than directly LF
SCRIPT_NAME = ` basename " $ 0 " `

# usage
usage () { echo "usage:" echo "$ SCRIPT_NAME old_text New_text" } if [ $ # -LT 2 ] ; Then     usage Exit -1 Fi text # replaces the front and rear of old_text = "$ 1" New_text = " $ 2 " OLD_TEXT_UPPER = ` echo $ old_text | TR '[AZ]' '[AZ]' ` # all uppercase old text OLD_TEXT_LOWER = ` echo $ old_text | TR '[AZ]' '[AZ]' ` # all-lowercase Old text NEW_TEXT_UPPER = ` echo $ New_text | TR '[AZ]' '[AZ]' ` # all uppercase new text NEW_TEXT_LOWER = ` echo $ New_text | TR '[AZ]' '[AZ]' ` # full lowercase The new text echo -e 'replace text & rename File ... \ N' # to find the specified file FIND_REGEX = '* (/ (Makefile |. readme) | \ (h |. Hxx | hpp | c | cpp | cxx | txt | mak | rc | x (ht) ml |? html |? sln | vcproj)) ' FILES = ` Find-type f-regextype POSIX-egrep-iregex "$ FIND_REGEX" ` replace text strings when the boundary # # SED_REGEX = '\ (\ b \ | _ \)' SED_REGEX = '\ (\ b \ | [0-9_] \)' # SED_REGEX = '\ (\ b \ | [0-9a-zA-Z_] \) ' # rename the file name of the boundary # GREP_REGEX = '(\ b | _)' GREP_REGEX = '(\ b | [0-9_])' # GREP_REGEX = '(\ b | [0-9a-zA-Z_ ]) ' # do find the file for each ... for EACH in $ FILES do # replace text in files OLD_TEXT is New_text     sed-i "s / $ SED_REGEX $ old_text $ SED_REGEX / \ 1 $ New_text \ 2 / g " $ EACH     sed-i "s / $ SED_REGEX $ OLD_TEXT_UPPER $ SED_REGEX / \ 1 $ NEW_TEXT_UPPER \ 2 / g" $ EACH     sed-i "s / $ SED_REGEX $ OLD_TEXT_LOWER $ SED_REGEX / \ 1 $ NEW_TEXT_LOWER \ 2 / g " $ EACH echo "$ EACH: replace: $ old_text => $ New_text" # rename a file containing OLD_TEXT called New_text OLD_FILE_0 = ` basename $ EACH | grep-E-i "$ GREP_REGEX $ old_text $ GREP_REGEX" ` # Only For the file name, regardless of the directory part if [ "$ OLD_FILE_0" ! = "" ] ; Then DIR = ` dirname $ EACH ` old_file = "$ DIR / $ OLD_FILE_0" NEW_FILE_0 = ` echo "$ OLD_FILE_0" | sed "s / $ old_text / $ New_text / gi " ` NEW_FILE = "$ DIR / $ NEW_FILE_0"         mv "$ old_file" "$ NEW_FILE" echo "rename: $ old_file => $ NEW_FILE" Fi echo '' DONE echo -e 'rename dir .. . \ N ' # change the directory name: Rename the directory name containing OLD_TEXT DIRS = ` Find-type d ` for EACH in $ DIRS ; do OLD_DIR_0 = ` basename $ EACH | grep-E-i "$ GREP_REGEX $ old_text $ GREP_REGEX " ` if [ "" $ OLD_DIR_0 ! = "" ] ; Then OLD_DIR_DIR = ` dirname $ EACH ` OLD_OLD_DIR = "$ OLD_DIR_DIR / $ OLD_DIR_0" NEW_DIR_0 = ` echo "$ OLD_DIR_0" | sed "s / $ old_text / $ New_text / gi " ` NEW_DIR_DIR = ` echo "$ OLD_DIR_DIR" | sed "s / $ old_text / $ New_text / gi" ` # Find the first output of the parent directory, then the parent directory has been renamed the # so OLD_DIR by the new parent directory name and OLD_DIR_0 spell, instead of the original OLD_OLD_DIR the OLD_DIR = "$ NEW_DIR_DIR / $ OLD_DIR_0" NEW_DIR = "$ NEW_DIR_DIR / $ NEW_DIR_0"         mv "$ OLD_DIR" "$ NEW_DIR" echo "rename: $ OLD_OLD_DIR => $ NEW_DIR" echo ' ' Fi DONE # Recovery Environment IFS = $ OLDIFS


Learn More :