拒绝labor 拥抱脚本~高效替换所有文件中字符串的Shell

使用场景: Linux或者Unix系统中,先查看下某个目录下所有文件列表:

tsadmin@tslnx10:~/com.ibm.rfidic.test.data/queries/set1.1/expected> ls
CVS
FVT_QG_EPCISCHG_0008_Expected_Response.xml
FVT_QG_EPCISCHG_0019_Expected_Response.xml
FVT_QG_EVENT_0001_Expected_Response.xml
FVT_QG_EVENT_0002_Expected_Response.xml
FVT_QG_EVENT_0003_Expected_Response.xml
FVT_QG_EVENT_0005_Expected_Response.xml
FVT_QG_EVENT_0009_Expected_Response.xml
FVT_QG_EVENT_0020_Expected_Response.xml
FVT_QG_EVENT_0025_Expected_Response.xml
FVT_QG_EVENT_0030_Expected_Response.xml
FVT_QG_EVENT_0031_Expected_Response.xml
FVT_QG_EVENT_0035_Expected_Response.xml
FVT_QG_EVENT_0038_Expected_Response.xml
FVT_QG_EVENT_0041_Expected_Response.xml
FVT_QG_EVENT_0045_Expected_Response.xml
FVT_QG_EVENT_0048_Expected_Response.xml
FVT_QG_EVENT_0056_Expected_Response.xml
FVT_QG_EVENT_0060_Expected_Response.xml
FVT_QG_EVENT_0065_Expected_Response.xml
FVT_QG_EVENT_0076_Expected_Response.xml
FVT_QG_EVENT_0077_Expected_Response.xml
FVT_QG_EVENT_0094_Expected_Response1.xml
FVT_QG_EVENT_0094_Expected_Response2.xml
FVT_QG_EVENT_0101_Expected_Response.xml
FVT_QG_EVENT_0103_Expected_Response.xml
FVT_QG_EVENT_0111_Expected_Response.xml
FVT_QG_EVENT_0112_Expected_Response.xml
FVT_QG_EVENT_0118_Expected_Response.xml
FVT_QG_EVENT_0130_Expected_Response.xml
FVT_QG_EVENT_0131_Expected_Response.xml
.................................

如果这些文件里都包含abc字符串,我想把他们全部替换成123,手工一个一个改,真是费力不讨好啊~ 不如写个脚本完成这个工作。

#!/bin/bash
if [ $# -ne 2 ];then
echo "usage: replaceKeyword.sh  "
exit 1
fi
 
SEARCH_KEYWORD=$1
REPLACE_STRING=$2
CURRENT_DIR=`pwd`
 
echo "Replacing $SEARCH_KEYWORD to $REPLACE_STRING in all files under $CURRENT_DIR starts ..."
for i in `ls`
do
grep $SEARCH_KEYWORD $i > /dev/null
if [ $? -eq 0 ];
then
echo "Replace keyword in $i"
sed "s/$SEARCH_KEYWORD/$REPLACE_STRING/g" $i > tmp
mv tmp $i
rm -f tmp
fi
done
echo "Finished."

实战下:我们希望把所有的<eventTimeZoneOffset>Z</eventTimeZoneOffset>替换成<eventTimeZoneOffset>+00:00</eventTimeZoneOffset>(注意要带双引号去除Shell对于>的特殊对待)

tsadmin@tslnx10:~/CVSVersion/vobs/RFIDICTest/fvt/cdlsrc/com.ib
m.rfidic.test.data/queries/set1.1/expected> ./replaceKeyword.sh ">Z<" ">+00:00<" Replacing >Z< to >+00:00< in all files under /home/tsadmin/CVSVersion/vobs/RFIDICTest/fvt/cdlsrc/com.ibm.rfidic.test.data/queries/set1.1/expected starts ...
Replace keyword in FVT_QG_EPCISCHG_0008_Expected_Response.xml
Replace keyword in FVT_QG_EVENT_0005_Expected_Response.xml
Replace keyword in FVT_QG_EVENT_0009_Expected_Response.xml
Replace keyword in FVT_QG_EVENT_0020_Expected_Response.xml
Replace keyword in FVT_QG_EVENT_0025_Expected_Response.xml
Replace keyword in FVT_QG_EVENT_0030_Expected_Response.xml
Replace keyword in FVT_QG_EVENT_0060_Expected_Response.xml
Replace keyword in FVT_QG_EVENT_0065_Expected_Response.xml
Replace keyword in FVT_QG_EVENT_0076_Expected_Response.xml
Replace keyword in FVT_QG_EVENT_0077_Expected_Response.xml
Replace keyword in FVT_QG_EVENT_0094_Expected_Response1.xml
Replace keyword in FVT_QG_EVENT_0094_Expected_Response2.xml
Replace keyword in FVT_QG_EVENT_0111_Expected_Response.xml
Replace keyword in FVT_QG_EVENT_0118_Expected_Response.xml
Replace keyword in FVT_QG_EVENT_0130_Expected_Response.xml
Replace keyword in LIQG008_17_Expected_Response.xml
Finished.

Leave a Comment.