Page 1 of 1

Rename files by shellscript

PostPosted: Wed Feb 13, 2019 8:11 am
by mediteran
Hi Community,

I have a bunch of files which I need to rename:

HDBB1A_ab.txt should become HDBB1_ab.txt

In other words, the 6th (sixth) character needs to be deleted. I've got two possible code lines:
Code: Select all
mv $1 $(echo $1 | sed 's/.//6')

and
Code: Select all
mv $1 $(echo $1 | sed 's6/.//')

But the one nor the other doesn't work. Any idea what I'm missing in the code?

Re: Rename files by shellscript

PostPosted: Wed Feb 13, 2019 10:24 am
by Mr_Noodle
How about using Hazel's pattern matching? Create a pattern with two custom attributes. One for the first 5 characters and one for the rest of the characters after the 6th. In the Rename pattern, you can then combine those.

Re: Rename files by shellscript

PostPosted: Thu Feb 14, 2019 9:39 am
by mediteran
This sounds great. As I saw, it’s a kind of “comfortable regex”. But I don’t get it to work.

I created a pattern with two attributes:

5 characters: “a a a a a1”
7+ characters: “_ab.txt”
Match the 1st occurrence from the beginning.

The syntax of the filenames is always the same:

4 letters
2 alphanumeric characters
the string “_ab.txt”

Re: Rename files by shellscript

PostPosted: Thu Feb 14, 2019 11:08 am
by Mr_Noodle
Your pattern seems off. It looks like there should be two "a1" instances. Also, not sure if it's in your actual pattern but it doesn't seem as if you want spaces in between each "a" token.

Re: Rename files by shellscript

PostPosted: Thu Feb 14, 2019 11:23 am
by mediteran
It looks like there should be two "a1" instances.

No, because the second "a1" instance is the 6th character which must be deleted.
Also, not sure if it's in your actual pattern but it doesn't seem as if you want spaces in between each "a" token.

The spaces are only in my forum response, not in the attribute:
Image

Re: Rename files by shellscript

PostPosted: Thu Feb 14, 2019 12:57 pm
by Mr_Noodle
The "a1" token only matches one character, either a letter or digit. If you have two characters in your example ("1A"), so you need two "a1" tokens.

Re: Rename files by shellscript

PostPosted: Thu Feb 14, 2019 1:22 pm
by mediteran
I tried now (and even earlier today) to work with three match attributes:
Image
whereas the second attribute looks like this:
Image
Again, no effect.

Re: Rename files by shellscript

PostPosted: Thu Feb 14, 2019 1:52 pm
by mediteran
Maybe this will help to understand it easier:
In InDesign GREP (or Regex) I would do the following:
Code: Select all
Find: (\w{5})\w(_ab\.txt)

In words: Find "attribute 1", "attribute 2", and "attribute 3"
Code: Select all
Replace: $1$2

in words: Replace with "attribute 1" and "attribute 3"

Re: Rename files by shellscript

PostPosted: Fri Feb 15, 2019 9:02 am
by mediteran
Finally, I succeeded!

Instead of "Name matches"
I now tried "Full Name matches"

and the pattern incl. rename works.