August 8th, 2003, 08:22 AM
Unix Script "File Change"
I am looking for a script that will change a certain character of a file name. I have a bunch of files from a location i.e "wf95234red3"
and i want to change the file to "wf96234red3" just changing the 6 . What i need is to change lots of these at the same time.
All the files have different letters at the end but the first part is the same wf95.
August 15th, 2003, 07:09 PM
August 16th, 2003, 05:49 PM
A small shell script should do the trick if you only want to change the 5 to a 6 (run this in the same directory as the wf95* files):
You might want to test it first by removing the '| sh' - it should output a list of 'mv' commands:
for file in `ls wf95*`
echo $file | sed -e 's/wf95\(.*\)/mv "&" wf96\1/' | sh
Even quicker you could just run the command from the commandline:
[23:47:22] root@users /root/tmp# ./tmp.sh
mv wf95234red3 wf96234red3
again though make sure the command is outputting what it should by removing the |sh first.
ls -1 wf95* | sed -e 's/wf95\(.*\)/mv "&" wf96\1/' | sh
Last edited by munkfish; August 17th, 2003 at 09:23 AM.
August 18th, 2003, 04:46 AM
That works fine, But i have a further problem.
I will also need to change the same thing inside the file also.
August 18th, 2003, 10:45 AM
Use the -i switch to edit the file 'in place':
To change every occurrence of 'wf95' with 'wf96' everywhere it occurs on each line.
sed -i -e 's/wf95/wf96/g' wf96*