Thursday, February 22, 2018

regex - How can I rename files in directory, while keeping part of the name unchanged?


I have multiple files (about 1000) named as such:


abcdefg123456.xyz
abcdefg123457.xyz
abcdefg123458.xyz
abcdefg123459.xyz

Some of the files have 4 additional random numbers and letters (in any order) after the name. These are possibly duplicates, but not always, so I need to change them to the original format to verify whether they are duplicate or not. They have this format:


abcdefg123456a789.xyz
abcdefg123457b987.xyz
abcdefg123458c879.xyz
abcdefg123459d897.xyz

On occasion, there is a wrong extension as well,


abcdefg123456.xyzedf
abcdefg123456.xyzfed

I want to rename these files to the original format of abcdefg followed by the original 6 numbers - i.e. to delete the trailing 4 random numbers and letters, and to delete the trailing extension back .xyz What I have so far is this:


rename -n "s/[a-z][0-9]{6}.xyz/.xyz/g"  *

But it doesn't seem to work. For some reason the output is:


abcdef.xyz (no numbers)

EDIT: I was a bit torn between which answer to choose from, because both helped in finding the solution. I went for stuts because he helped with the second part of the question as well. But your help is greatly appreciated too Mark Perryman - and the commenters as well of course.


Answer



Solution


To remove the 4 numbers/letters preceding the full stop for all files you can use the following loop:


for file in *.xyz ; do
NEWFILE=$(echo "$file" |sed -re 's/[a-z|0-9][a-z|0-9][a-z|0-9][a-z|0-9](\.)/\./g')
mv -v $file $NEWFILE
done

Explanation


for file in *.xyz ; do

Loops through every file with a .xyz extension


NEWFILE=$(echo "$file" |sed -re 's/[a-z|0-9][a-z|0-9][a-z|0-9][a-z|0-9](\.)/\./g')

Create a variable called NEWFILE containing the name of the file after stripping out a pattern that matches [a-z|0-9][a-z|0-9][a-z|0-9][a-z|0-9] (a mix of 4 numbers or letters)and is followed by a full stop ((\.)).


mv -v $file $NEWFILE

Move the file to its new name, the -v will print the move process in the following format


`abcdefg123456a789.xyz` -> `abcdefg123456.xyz`

This currently does not cover the fixing of extensions but a similar solution to the above can be used but with the sed command being sed 's/\.xyz.*/\.xyz/g'.


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...