vaibhavmishra062201
asked on
replace string in all the files
Hi,
I want to replace a string "abc" with "xyz" in all the files, in current directory and all its subdirectories. How to do that ?
I do not want to open each and every file and replace the string.
Thanx in advance,
Vaibhav
I want to replace a string "abc" with "xyz" in all the files, in current directory and all its subdirectories. How to do that ?
I do not want to open each and every file and replace the string.
Thanx in advance,
Vaibhav
oops
ls -R <name of directory to which you want to apply changes>| while read i
do
sed -e 's/abc/xyz/g' $i > $i.new
done
moreover.. this will store modified files in filename.new
you can execute a mv in similar loop to replace old files with new ones
ls -R | while read i
do
sed -e 's/abc/xyz/g' $i > $i.new
mv -f $i.new $i
done
ls -R <name of directory to which you want to apply changes>| while read i
do
sed -e 's/abc/xyz/g' $i > $i.new
done
moreover.. this will store modified files in filename.new
you can execute a mv in similar loop to replace old files with new ones
ls -R | while read i
do
sed -e 's/abc/xyz/g' $i > $i.new
mv -f $i.new $i
done
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi sunnycoder
it is creating al the .new files in current folder ...
can you please modify the script so that .new file gets created in the folder same as the original file .. ?
Vaibhav
it is creating al the .new files in current folder ...
can you please modify the script so that .new file gets created in the folder same as the original file .. ?
Vaibhav
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok ... i ran it on a test folder and it is working fine ..
do you want to share the points with glassd ? if yes how many ?
do you want to share the points with glassd ? if yes how many ?
I will add my usual comment about checking the status before renaming the file, otherwise you can end up nobbling loads of files
sed 's/abc/xyz/g' $i >$i.new && mv $i.new $i
sed 's/abc/xyz/g' $i >$i.new && mv $i.new $i
>do you want to share the points with glassd ? if yes how many ?
you have to decide if you want to split and the ratio too
you have to decide if you want to split and the ratio too
try this
ls -R | while read i
do
sed -e 's/abc/xyz/g' $i
done
Cheers!
Sunny:o)