批量替换

replace.bat

@echo off 
echo 开始替换! 
setlocal enabledelayedexpansion 
set TMPFILE2=%random%.reg 
set TMPFILE3=%random%.reg 
set TMPFILE4=%random%.reg 
if exist %TMPFILE2% ( del /f/q %TMPFILE2% ) 
if exist %TMPFILE3% ( del /f/q %TMPFILE3% ) 
if exist %TMPFILE4% ( del /f/q %TMPFILE4% ) 
reg export HKEY_CURRENT_USERSoftwarecvsntcvspass %TMPFILE2% 
if %ERRORLEVEL% EQU 0 ( 
type %TMPFILE2% > %TMPFILE3% 
for /f "tokens=*" %%m in ( 'findstr "172.20.23.159 172.20.16.8 172.16.40.2 172.16.2.130 ***.10.67.172 ***.10.67.171 ***.239.1***.75 ***.239.1***.74" %TMPFILE3%' ) do ( 
if "%%m"=="" else ( 
for /f "tokens=*" %%l in ( %TMPFILE3% ) do ( 
set tmp2=%%l 
set "tmp2=!tmp2:172.20.23.159=scm1.***.cn!" 
set "tmp2=!tmp2:172.20.16.8=scm2.***.cn!" 
set "tmp2=!tmp2:172.16.40.2=scm3.***.cn!" 
set "tmp2=!tmp2:172.16.2.130=scm4.***.cn!" 
set "tmp2=!tmp2:***.10.67.172=scm1.***.cn!" 
set "tmp2=!tmp2:***.10.67.171=scm2.***.cn!" 
set "tmp2=!tmp2:***.239.1***.75=scm3.***.cn!" 
set "tmp2=!tmp2:***.239.1***.74=scm4.***.cn!" 
echo !tmp2!>>%TMPFILE4% 
) 
reg delete HKEY_CURRENT_USERSoftwarecvsntcvspass /va /f 
reg import %TMPFILE4% 
del /f/q %TMPFILE4% 
) 
) 
del /f/q %TMPFILE3% 
del /f/q %TMPFILE2% 
) 
set execdir=%~dp0 
cd /d %execdir% 
set TMPFILE=%random%.tmp 
if exist %TMPFILE% ( del /f/q %TMPFILE% ) 
for /r . %%i in ( Root ) do if exist "%%i" ( 
for /f "tokens=*" %%j in ( 'findstr "172.20.23.159 172.20.16.8 172.16.40.2 172.16.2.130 ***.10.67.172 ***.10.67.171 ***.239.1***.75 ***.239.1***.74" "%%i"' ) do ( 
if "%%j"=="" else ( 
for /f "usebackq tokens=*" %%k in ( "%%i" ) do ( 
echo "%%i" 
set tmp=%%k 
set "tmp=!tmp:172.20.23.159=scm1.***.cn!" 
set "tmp=!tmp:172.20.16.8=scm2.***.cn!" 
set "tmp=!tmp:172.16.40.2=scm3.***.cn!" 
set "tmp=!tmp:172.16.2.130=scm4.***.cn!" 
set "tmp=!tmp:***.10.67.172=scm1.***.cn!" 
set "tmp=!tmp:***.10.67.171=scm2.***.cn!" 
set "tmp=!tmp:***.239.1***.75=scm3.***.cn!" 
set "tmp=!tmp:***.239.1***.74=scm4.***.cn!" 
echo !tmp!>>%TMPFILE% 
move /y "%%i" "%%i".bak 
move /y %TMPFILE% "%%i" 
) 
) 
) 
) 
endlocal 
echo 替换完毕! 
pause

replace.sh

#!/bin/sh 
echo begin replace! 
if [ -f $HOME/.cvspass ] 
then 
if grep -E "172.20.23.159|172.20.16.8|172.16.40.2|172.16.2.130|***.10.67.172|***.10.67.171|***.239.1***.75|***.239.1***.74" $HOME/.cvspass 
then 
sed -i.bak "s/172.20.23.159/scm1.***.cn/;s/172.20.16.8/scm2.***.cn/;s/172.16.40.2/scm3.***.cn/;s/172.16.2.130/scm4.***.cn/;s/***.10.67.172/scm1.***.cn/;s/***.10.67.171/scm2.***.cn/;s/***.239.1***.75/scm3.***.cn/;s/***.239.1***.74/scm4.***.cn/" $HOME/.cvspass 
fi 
fi 
SAVEIFS=$IFS 
#IFS=$(echo -en "nb") 
IFS=" 
" 
exedir=`dirname $0` 
cd $exedir 
files=`find . -name Root -follow` 
for f in $files 
do 
g=`cat "$f"` 
h=`echo ${g##*@}` 
i=`echo ${h%%:*}` 
case $i in 
172.20.23.159) 
echo "$f" 
sed -i.bak "s/172.20.23.159/scm1.***.cn/" "$f" 
;; 
172.20.16.8) 
echo "$f" 
sed -i.bak "s/172.20.16.8/scm2.***.cn/" "$f" 
;; 
172.16.40.2) 
echo "$f" 
sed -i.bak "s/172.16.40.2/scm3.***.cn/" "$f" 
;; 
172.16.2.130) 
echo "$f" 
sed -i.bak "s/172.16.2.130/scm4.***.cn/" "$f" 
;; 
***.10.67.172) 
echo "$f" 
sed -i.bak "s/***.10.67.172/scm1.***.cn/" "$f" 
;; 
***.10.67.171) 
echo "$f" 
sed -i.bak "s/***.10.67.171/scm2.***.cn/" "$f" 
;; 
***.239.1***.75) 
echo "$f" 
sed -i.bak "s/***.239.1***.75/scm3.***.cn/" "$f" 
;; 
***.239.1***.74) 
echo "$f" 
sed -i.bak "s/***.239.1***.74/scm4.***.cn/" "$f" 
;; 
esac 
done 
IFS=$SAVEIFS 
echo replace finished!

你可能感兴趣的:(批量替换)