1752777 Members
6115 Online
108789 Solutions
New Discussion юеВ

Re: convert filenames

 
SOLVED
Go to solution
Christian Marquardt_1
Regular Advisor

convert filenames

hello@all,
I have some files with date in filename. Now I#ve to convert the date format in the filename from DD.MM.YYYY to YYYYMMDD.
Sample:
original filename is: BA_D07.12.2006.txt
after converting it should be: BA_D20061207.txt

Can anyone help me? I've no idea how to do.

kind regards
Christian
4 REPLIES 4
Peter Godron
Honored Contributor
Solution

Re: convert filenames

Christian,
a bit more info required, such as fix format ?

Otherwise:
file="BA_D05.12.2006.txt"
new_file=`echo $file | cut -c 1-4``echo $file | cut -c11-14``echo $file | cut -c8-9``echo $file | cut -c5-6``echo $file | cut -c15-`
echo $new_file
BA_D20061205.txt
Dennis Handly
Acclaimed Contributor

Re: convert filenames

What you need to do is use sed in a for-loop:
for file in BA_D??.??.????.txt; do
echo mv $file $(echo $file | \
sed -e 's/BA_D\(..\)\.\(..\)\.\(....\)/BA_D\3\2\1/')
done

Remove the "echo" above when you are sure that the mv commands are proper.

The for-loop uses shell pattern matching regexp(5). sed uses basic regular expressions regexp(5).

\( and \) bracket chunks that can be referenced by \#, their position. And I need to quote the "." since they are special.
Christian Marquardt_1
Regular Advisor

Re: convert filenames

Dennis' way works fine.
It's a very strange script but it works fine ;-)

Thanks a lot!!!!

kind regards
Christian
Christian Marquardt_1
Regular Advisor

Re: convert filenames

it works