Page 1 of 2

Find and replace with pattern matching/tokens

PostPosted: Mon Apr 27, 2020 5:14 pm
by united
I was trying to do the following was wondering if it even possible with hazel? (?pattern match and custom token)?

Edit: replaced the test files and setup environment.

ex.
3232 The ddafddfdd, 6666 the brave123.txt
files 4324 abce addfsaa, 6666 the brave343.txt
Tddfdsadahe feddfdw, 6666 the 3439 brave234.txt

desired:
(3232) The ddafddfdd, 6666 the brave123.txt
files (4324) abce addfsaa, 6666 the brave343.txt
Tddfdsadahe feddfdw, 6666 the (3439) brave234.txt


setup: Open terminal and add the following
touch "3232 The ddafddfdd, 6666 the brave123.txt"
touch "files 4324 abce addfsaa, 6666 the brave343.txt"
touch "Tddfdsadahe feddfdw, 6666 the 3439 brave234.txt"


Overview
- match any 4 digit number except 6666.
- do not match 6666
- add "(" ")" around the matched 4 digit number.

Issue:
- Even with setting up a pattern match and token it renames the entire filename not just the pattern that is matched.

Or would I have to use a script and embed it in hazel?

-

Re: Find and replace with pattern matching/tokens

PostPosted: Tue Apr 28, 2020 9:20 am
by Mr_Noodle
It should be doable. Can you post your rule?

Re: Find and replace with pattern matching/tokens

PostPosted: Tue Apr 28, 2020 11:54 am
by united
Here's the rule:
https://www.mediafire.com/file/ranyynm1 ... e.zip/file - hazel file zipped

Also, added a setup to create the source files and more accurate use cases in the first post

issue is the following:

source:
3232 The ddafddfdd, 6666 the brave123.txt
files 4324 abce addfsaa, 6666 the brave343.txt
Tddfdsadahe feddfdw, 6666 the 3439 brave234.txt

Desired
(3232) The ddafddfdd, 6666 the brave123.txt
files (4324) abce addfsaa, 6666 the brave343.txt
Tddfdsadahe feddfdw, 6666 the (3439) brave234.txt

Actual:
(3232).txt
(4324).txt
(6666).txt

Question:
- how do you replace the matched pattern without rename the entire file?
- how do you exclude the 6666 pattern matched from the rename.

thanks

Re: Find and replace with pattern matching/tokens

PostPosted: Wed Apr 29, 2020 9:37 am
by Mr_Noodle
Your rule is not showing up but it seems like you are not accounting for the full filename. You need to both capture the rest of the filename and then use that in the rename pattern. You can't just arbitrarily use a part of it as there is no way for Hazel to know what part you mean.

Re: Find and replace with pattern matching/tokens

PostPosted: Wed Apr 29, 2020 12:29 pm
by united
Ok uploaded the file again.

Re: Find and replace with pattern matching/tokens

PostPosted: Wed Apr 29, 2020 12:30 pm
by united
Mr_Noodle wrote:Your rule is not showing up but it seems like you are not accounting for the full filename. You need to both capture the rest of the filename and then use that in the rename pattern. You can't just arbitrarily use a part of it as there is no way for Hazel to know what part you mean.


I understand that but the replace within the text option doesn't seem to be available. unless I'm not seeing it in hazel?

Re: Find and replace with pattern matching/tokens

PostPosted: Thu Apr 30, 2020 10:16 am
by Mr_Noodle
You need to capture the rest of the filename into its own custom attribute and use that in the Rename action. Again, Hazel can't know what that is unless you specify it.

Re: Find and replace with pattern matching/tokens

PostPosted: Thu Apr 30, 2020 2:13 pm
by united
Mr_Noodle wrote:You need to capture the rest of the filename into its own custom attribute and use that in the Rename action. Again, Hazel can't know what that is unless you specify it.


I might be unclear in how to approach this.

I have:
1 - custom text - 4digit > [...][1][1][1][1][...]

I need to create another
2 - custom text2 - All > [...]

Question
Is how do I specify that "4digit" replace text in "All"? I'm not seeing a replace feature for that in hazel unless i'm mistaken..

Re: Find and replace with pattern matching/tokens

PostPosted: Fri May 01, 2020 9:24 am
by Mr_Noodle
You need to create an attribute for the rest of the name, not all. Then you have two attributes. One for the initial digits and another for what comes after it.

Re: Find and replace with pattern matching/tokens

PostPosted: Fri May 01, 2020 12:22 pm
by united
Mr_Noodle wrote:You need to create an attribute for the rest of the name, not all. Then you have two attributes. One for the initial digits and another for what comes after it.


Hazel rules File:
https://www.mediafire.com/file/ehaefw6q ... 2.zip/file

Rule:
name matches
-- custom attribute1: beginning > ... (anything)
-- custom attribute2: 4digit > [1][1][1][1] (digit)
-- custom attribute3: ending > ... (anything)

Replace rule
rename
[beginning]([4digit])[ending][extension]

Desired
(3232) The ddafddfdd, 6666 the brave123.txt
files (4324) abce addfsaa, 6666 the brave343.txt
Tddfdsadahe feddfdw, 6666 the (3439) brave234.txt

Actual:
(3232) The ddafddfdd, 6666 the brave123.txt
files (4324) abce addfsaa, 6666 the brave343.txt
Tddfdsadahe feddfdw, (6666) the 3439 brave234.txt <-- how do you ignore the 6666 matching

Mr_Noodle wrote:Then you have two attributes. One for the initial digits and another for what comes after it.


question:
? - How do you create only 2 attributes when you need three which includes the beginning, middle and ending text?
? - how do you ignore the 6666 (6666 is for simplification but it can be any 4 digit number specified to be ignored) match

Re: Find and replace with pattern matching/tokens

PostPosted: Mon May 04, 2020 10:12 am
by Mr_Noodle
I overlooked the examples showing text preceding the numbers.

As for ignoring the first set of numbers, is this always the case? How do you determine, as a human, which set of 4 numbers is the one you find important?

Re: Find and replace with pattern matching/tokens

PostPosted: Mon May 04, 2020 11:20 am
by united
Mr_Noodle wrote:I overlooked the examples showing text preceding the numbers.

As for ignoring the first set of numbers, is this always the case? How do you determine, as a human, which set of 4 numbers is the one you find important?


Yes, the specified number 6666 or specified should always be ignored. As a human any numbers that is not 6666 or specified shouldn't be processed in the name. Any other 4 digit numbers can be processed.

Re: Find and replace with pattern matching/tokens

PostPosted: Tue May 05, 2020 10:08 am
by Mr_Noodle
I suggest you have another pattern which takes the 6666 into account then.

Re: Find and replace with pattern matching/tokens

PostPosted: Wed May 06, 2020 3:52 am
by united
Mr_Noodle wrote:I suggest you have another pattern which takes the 6666 into account then.



Ok so i take it you suggest need another rule that changes the (6666) back to 6666. right?

But what about the number in example 3

Desired:
Tddfdsadahe feddfdw, 6666 the (3439) brave234.txt

Actual:
Tddfdsadahe feddfdw, (6666) the 3439 brave234.txt <-- how do you ignore the 6666 matching

Re: Find and replace with pattern matching/tokens

PostPosted: Wed May 06, 2020 9:22 am
by Mr_Noodle
Add 6666 to the pattern. Like: (...)6666(...)(•your custom attribute matching 4 digits)(...)