행복아울렛
[UNIX/LINUX] Shell Script for changing all string in a directory recursivly! 본문
[UNIX/LINUX] Shell Script for changing all string in a directory recursivly!
붕탱구 2007. 1. 5. 09:25
A Shell Script For Changing Oldstring to NewString in a directory recursively.
This code, which we will call ach(All Change), finds all files whose extension is one of .c, .h, .s .cpp and contain the OldString. And It Chanes OldString to New String in one breath.
It's very useful!
USAGE:
$ach oldstring newstring
REQUIREMENTS:
vim6.0 or above
sed
grep
CODE:
#!/bin/sh
# Check parameters
if [ "$#" -ne 2 ]
then
echo "All Change Oldstring to Newstring in a directory recursively"
echo "usage:ach Oldstring Newstring"
exit
fi
if [ -f ./vim.scr ]
then
echo "vim.scr already exists!"
rm -rf ./vim.scr
echo "vim.scr removed!"
exit 1
else
touch ./vim.scr
echo ":%s,\<$1\>,$2,g">> vim.scr
echo ":wq">> vim.scr
find ./ \( -name '*.c' -o -name '*.h' -o -name '*.s' -o -name '*.cpp' \) -print > allfiles
temp1=`cat allfiles`
for files in `echo $temp1`
do
grep -l "\<$1\>" $files /dev/null | sed 's/.*/vim & -s vim.scr/' >> tmpfiles
done
if [ -s tmpfiles ]
then
sh tmpfiles
else
echo "ach:No file to chage"
fi
rm -rf ./vim.scr
rm -rf ./tmpfiles
rm -rf ./allfiles
fi