Results 1 to 5 of 5
Hi,
Im looking for assistance to create a script to find and replace files. Probably best if I give you the background
Our server uses a specific application which stores ...
- 09-28-2010 #1Just Joined!
- Join Date
- Sep 2010
- Posts
- 2
Find & Replace Script in Bash
Hi,
Im looking for assistance to create a script to find and replace files. Probably best if I give you the background
Our server uses a specific application which stores user data, each user data account (a folder on the server) has a file called 'Profile.xml' this file gets updated and replaced about every 30 mins similar to the fashion logrotate works i.e. Profile.xml.1 Profile.xml.2 -> .10
What we experience is that if the application crashes unexpectedly while it is doing its user profile refresh task we end up with sometimes a few hundred Profile.xml files which end up 0kb(should be around 4kb) , and our server see's these as corrupted profiles and will not see them. Our fix is to go back thru and rename the Profile.xml.1 to be Profile.xml (or sometimes up to Profile.xml.5 to Profile.xml)
We want a script we can manually run to automate this process
The server tree is
/mnt/array1/username/db/Profile.xml
/mnt/array2/username/db/Profile.xml
etc
etc
What we have so far is a script which finds the affected files
find /mnt/ -maxdepth 4 -name Profile.xml -size -1k
This will display a list of affected profiles, and we can append it to a text file with >>output.txt on the end.
ive investigated a few options using cp, mv, or sed and none seem to achieve this easily
perhaps some kind of script that uses a if statement?
like
if 'pattern' in 'location' equals '0kb' then 'cp' Profile.xml.1 Profile.xml
sorry my scripting is not so good, can anyone make a suggestion?
any help appreciated
- 09-28-2010 #2Just Joined!
- Join Date
- Jan 2010
- Posts
- 12
maybe you could try like this
this is not so good but it is goot to be an exampleCode:ALLFILES=`find /mnt/ -maxdepth 4 -name Profile.xml -size -1k` while READ FILE; do cp $FILE".1" $FILE done < $ALLFILES
- 09-28-2010 #3Just Joined!
- Join Date
- Sep 2010
- Posts
- 6
here is my script, hope it helps:
On my machine I have a pretty good utility called replace.#! /bin/bash
if [ $# -ne 3 ]
then
echo "Usage - $0 file target replacement"
exit 1
fi
sed -e "s/$2/$3/g" $1 > $1__new
echo "$1__new Done"
Take a look to the Linux man page.Last edited by aihaike; 09-28-2010 at 05:44 AM.
- 09-28-2010 #4Just Joined!
- Join Date
- Sep 2010
- Posts
- 2
- 09-29-2010 #5Linux User
- Join Date
- Nov 2009
- Location
- France
- Posts
- 292
This assumes that Profile.xml.1 is not empty. If necessary, loop over Profile.xml.x, test for size of Profile.xml.x before restoring.
Code:ALLFILES=$(find /mnt/ -maxdepth 4 -name "Profile.xml" -size -1k) for f in $ALLFILES do cp -f "$f.1" $f done
0 + 1 = 1 != 2 <> 3 != 4 ...
Until the camel can pass though the eye of the needle.


Reply With Quote
