3. OS이야기
Unix 에서 특정문자열이 포함된 모든 파일의 내용을 일괄 변경하기
OSSW(Open Source System SoftWare
2009. 6. 2. 15:48
1. test 라는 문자열이 포함된 모든 파일을 찾는다
find . -type f | xargs grep -l test > ttt.txt
2. 아래 스크립트를 이용하여 test 문자열을 test2 로 변환한다.
./rep.sh ttt.txt test test2
cat /tmp/rep.sh
-----------------------------------
#!/bin/sh
if [ $# != 3 ];then
echo "Uages: /tmp/rep.sh <filelist> <old_str> <new_str>"
exit;
fi
set -x
mkdir -p /tmp/tt
for file in `cat $1`
do
echo ""
echo $file
cat $file | sed -e "s/$2/$3/g" > /tmp/tt/temp.txt
mv /tmp/tt/temp.txt $file
done
set +x
-------------------------------------