shell替換程式裡的程式碼

weixin_33858249發表於2017-11-12

需求:
副檔名型別: .php .html .shtml .inc
http://market.pk.com/files/union/ad_union_5.js替換為http://market.pk.com/files/union/union_5.js
http://market.pk.com/files/advertisement/27/ad_27.js替換http://market.pk.com/files/upload/27/27.js

http://market.pk.com/files/union/ad_union_(*).js
替換為:http://market.pk.com/files/union/union_(*).js
http://market.pk.com/files/advertisement/(*)/ad_(*).js
替換為:http://market.pk.com/files/upload/(*)/(*).js


指令碼1:
#!/bin/bash

ROOT_DIR='/root/temp/wow'
KEYWORD='ad_union'
KEYWORD2='advertisement'

#for i in $(find $ROOT_DIR -name "*.*" -exec grep "$KEYWORD" {} \;)
for i in $(find $ROOT_DIR -name "*.*")
do
ifile=$(grep "$KEYWORD" $i)
if [ "$?" -eq "0" ]
then
FILE_NUMBER=$(echo $ifile | awk -F"\"" '{print $2}' | awk -F"/" '{print $NF}' | awk -F"." '{print $1}' | awk -F"_" '{print $NF}')
#sed -i "s#ad\_union\_[0-9][0-9]#union\_$FILE_NUMBER#g" $i
sed -i "s#ad\_union\_$FILE_NUMBER#union\_$FILE_NUMBER#g" $i
fi

ifile=$(grep "$KEYWORD2" $i)
if [ "$?" -eq "0" ]
then
FILE_NUMBER2=$(echo $ifile | awk -F"\"" '{print $2}' | awk -F"/" '{print $NF}' | awk -F"." '{print $1}' | awk -F"_" '{print $NF}')
sed -i "s#advertisement\/$FILE_NUMBER2\/ad\_$FILE_NUMBER2#upload\/$FILE_NUMBER2\/$FILE_NUMBER2#g" $i
fi
done

 

 


指令碼2:
#!/bin/bash

EXT_ARRAY=("*.php" "*.inc" "*.html" "*.shtml")
EXT_ARRAY_LEN=${#EXT_ARRAY[@]}
ROOT_DIR='/root/temp/wow'
KEYWORD='ad_union'
KEYWORD2='advertisement'

#i=0
#while [ $i -lt $EXT_ARRAY_LEN ]
#do
# find $ROOT_DIR -name "${EXT_ARRAY[$i]}" | while read f
# do
# sed -i 's#union\/ad_union_5\.js#union\/union_5\.js#g' $f
# sed -i 's#advertisement\/27\/ad_27\.js#upload\/27\/27\.js#g' $f
# done
# let i++
#done

#for i in $(find $ROOT_DIR -name "*.*" -exec grep "$KEYWORD" {} \;)
for i in $(find $ROOT_DIR -name "*.*")
do
ifile=$(grep "$KEYWORD" $i)
if [ "$?" -eq "0" ]
then
FILE_NUMBER=$(echo $ifile | awk -F"/" '{print $NF}' | awk -F"." '{print $1}' | awk -F"_" '{print $NF}')
#sed -i "s#ad\_union\_[0-9][0-9]#union\_$FILE_NUMBER#g" $i
sed -i "s#ad\_union\_$FILE_NUMBER#union\_$FILE_NUMBER#g" $i
fi

ifile=$(grep "$KEYWORD2" $i)
if [ "$?" -eq "0" ]
then
FILE_NUMBER2=$(echo $ifile | awk -F"/" '{print $NF}' | awk -F"." '{print $1}' | awk -F"_" '{print $NF}')
sed -i "s#advertisement\/$FILE_NUMBER2\/ad\_$FILE_NUMBER2#upload\/$FILE_NUMBER2\/$FILE_NUMBER2#g" $i
fi
done

這兩個指令碼分別執行替換程式裡的程式碼內容。

本文轉自 liang3391 51CTO部落格,原文連結:http://blog.51cto.com/liang3391/456622


相關文章