Operating System - HP-UX
1825766 Members
2095 Online
109687 Solutions
New Discussion

Re: Script help, just to make a change :)

 
SOLVED
Go to solution
George_Dodds
Honored Contributor

Script help, just to make a change :)

I need a script to rename filenames of the following format to the same filename but just omitting the colon in the middle.

2003_08_27_NE:31.all.cdl

The datestamp varies as do the two digits after NE:

Cheers

George



8 REPLIES 8
Rodney Hills
Honored Contributor

Re: Script help, just to make a change :)

A short perl script-

cd /the/folder
ls | perl -ne 'chomp;$orig=$_;s/://;system("mv $orig $_")'

This assumes that their will be no conflict in names (ie duplicate names).

HTH

-- Rod Hills
There be dragons...
John Poff
Honored Contributor

Re: Script help, just to make a change :)

Hi George,

Here is one way to do it:

for oldfile in *:*
do
newfile=$(echo $oldfile | sed s/\://)
echo "Renaming $oldfile to $newfile"
mv $oldfile $newfile
done



JP
A. Clay Stephenson
Acclaimed Contributor

Re: Script help, just to make a change :)

Easy,

#!/usr/bin/sh

ls [0-9][0-9][0-9][0-9]_[0-9][0-9]_[0-9][0-9]_NE:[0-9][0-9].all.cdl | while read X
do
NEWNAME=$(echo ${X} | tr -d ":")
mv ${X} ${NEWNAME}
done


If it ain't broke, I can fix that.
Tomek Gryszkiewicz
Trusted Contributor
Solution

Re: Script help, just to make a change :)

do something like:

#!/bin/sh
for name in `ls *:*`
do
ch_name=`echo $name|awk -F':' '{print $1$2;}'`
mv $name $ch_name
done


-Tomek
George_Dodds
Honored Contributor

Re: Script help, just to make a change :)

Job done cheers peeps :)

Ta

George
George_Dodds
Honored Contributor

Re: Script help, just to make a change :)

just another quickie , i'm using Tomek's script (cheers)

#!/bin/sh
for name in `ls *:*`
do
ch_name=`echo $name|awk -F':' '{print $1$2;}'`
mv $name $ch_name
done

Could i use sed to replace the .all.cdl ending with .txt ?

My dodgy book only talk about using sed to edit contents of a file not the filename itself.


Cheers

George

A. Clay Stephenson
Acclaimed Contributor

Re: Script help, just to make a change :)

Sure, you can remove the colon and replace the .all.cdl in one sed operation. Note: I will still use my original pattern as it is the only one that exactly matches your original file pattern:

Easy,
#!/usr/bin/sh
ls [0-9][0-9][0-9][0-9]_[0-9][0-9]_[0-9][0-9]_NE:[0-9][0-9].all.cdl | while read X
do
NEWNAME=$(echo ${X} | sed -e 's/.all.cdl$/.txt/' -e 's/://g')
mv ${X} ${NEWNAME}
done

Note the use of the '$' to anchor .all.cdl at the end of the string so that it will not replace the same pattern anywhere else in the string --- although in this case with the restricted ls pattern you couldn't actually get .all.cdl anywhere but at the terminal end of the filename.


A good idea while you are getting your patterns exactly like you want them is to replace (or comment out) the mv command andf replace it with an echo, viz,
echo "${X} ---> ${NEWNAME}"
If it ain't broke, I can fix that.
George_Dodds
Honored Contributor

Re: Script help, just to make a change :)

Cool

Cheers Clay