Skip to main content
Inspiring
October 4, 2019
Answered

Remove duplicates separated by commas – GREP or Script help

  • October 4, 2019
  • 4 replies
  • 2978 views

I'm working on a catalog using data merge. The data is coming in like this:

Black, Black, Black, Yellow, Yellow, Green, Blue, Blue, Blue, Blue, Red, Red, Brown, Purple, Purple, Purple, Purple, Purple

Can someone help with a Script or GREP that will remove the duplicates and leave me with this?:

Black, Yellow, Green, Blue, Red, Brown, Purple

 

Also, there are many more color combinations and the duplication can be up to 12 times in a string of text.

 

Any help from the community is greatly appreciated!!

 

 

Correct answer jimd97292894

Final AppleScript based on winterm's GREP. I added the repeat loop and placed it at 12 because 12 is the maximum number of times a color will repeat in my project. If this is used as a GREP only ((\w+ )*\w+, )\1, you have to run it multiple times to work.

 

tell application "Adobe InDesign CC 2019"

repeat 12 times

set find grep preferences to nothing

set change grep preferences to nothing

set find what of find grep preferences to "((\\w+ )*\\w+, )\\1"

set change to of change grep preferences to "$1"

change grep

set find grep preferences to nothing

set change grep preferences to nothing

end repeat

end tell

 

 

4 replies

jimd97292894AuthorCorrect answer
Inspiring
October 7, 2019

Final AppleScript based on winterm's GREP. I added the repeat loop and placed it at 12 because 12 is the maximum number of times a color will repeat in my project. If this is used as a GREP only ((\w+ )*\w+, )\1, you have to run it multiple times to work.

 

tell application "Adobe InDesign CC 2019"

repeat 12 times

set find grep preferences to nothing

set change grep preferences to nothing

set find what of find grep preferences to "((\\w+ )*\\w+, )\\1"

set change to of change grep preferences to "$1"

change grep

set find grep preferences to nothing

set change grep preferences to nothing

end repeat

end tell

 

 

winterm
Brainiac
October 4, 2019

I'd prefer this AIO:

Find:

(\w+, )\1

Change to:

$1

Repeat Change All untill no matches will be found.

Inspiring
October 4, 2019

Any fix if there are also TWO or THREE words separated by commas? If example: "Light Blue Grey, Light Blue Grey, Light Blue Grey, Dark Red, Dark Red, Green, Green, Green, Green"

The GREP doesn't seem to work in that instance.

winterm
Brainiac
October 4, 2019
No surprise, since it's based on a given samples. Try this ((\w+ )*\w+, )\1 or this ([^,]+, )\1 Slightly different approach, but result ought to be essentially the same.
Brainiac
October 4, 2019

Here's a GREP, but it will have to run separately for each color that has duplicates in the document. 
Just be sure to add the space in the find and replace fields.

Inspiring
October 4, 2019

Thanks! I'll give that a try!! That's the starting point I was looking for, I should be able to write an AppleScript based on that.

Brainiac
October 4, 2019

If your data merge is creating this problem, it should be fixable by editing your merge template or your data file.

Inspiring
October 4, 2019

Unfortunately, it's being created in the Excel file that I'm grabbing the data from. According to our Excel guru it can't be easily fixed on his end.