Find and replace with pattern matching/tokens

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Find and replace with pattern matching/tokens 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?

-
Last edited by united on Tue Apr 28, 2020 12:13 pm, edited 4 times in total.
united
 
Posts: 54
Joined: Wed Mar 14, 2012 3:48 pm

It should be doable. Can you post your rule?
Mr_Noodle
Site Admin
 
Posts: 11250
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

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
Last edited by united on Wed Apr 29, 2020 12:33 pm, edited 4 times in total.
united
 
Posts: 54
Joined: Wed Mar 14, 2012 3:48 pm

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.
Mr_Noodle
Site Admin
 
Posts: 11250
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Ok uploaded the file again.
united
 
Posts: 54
Joined: Wed Mar 14, 2012 3:48 pm

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?
united
 
Posts: 54
Joined: Wed Mar 14, 2012 3:48 pm

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.
Mr_Noodle
Site Admin
 
Posts: 11250
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

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..
united
 
Posts: 54
Joined: Wed Mar 14, 2012 3:48 pm

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.
Mr_Noodle
Site Admin
 
Posts: 11250
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

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
united
 
Posts: 54
Joined: Wed Mar 14, 2012 3:48 pm

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?
Mr_Noodle
Site Admin
 
Posts: 11250
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

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.
united
 
Posts: 54
Joined: Wed Mar 14, 2012 3:48 pm

I suggest you have another pattern which takes the 6666 into account then.
Mr_Noodle
Site Admin
 
Posts: 11250
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

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
united
 
Posts: 54
Joined: Wed Mar 14, 2012 3:48 pm

Add 6666 to the pattern. Like: (...)6666(...)(•your custom attribute matching 4 digits)(...)
Mr_Noodle
Site Admin
 
Posts: 11250
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Next

Return to Support

cron