Replace keyword with new value or add if not present

Started by Elazul, January 31, 2020, 06:20:32 AM

Previous topic - Next topic

Elazul

I have a keywords list that looks similar to this:
Keywords                        : Merlin ID:139777188, 20190227, REMOTE, "Merlin Crop: 0 0 2489 1376"
I need to add a new "Merlin Crop: ? ? ? ?" keyword instead of existing one(if present). The hard part is to do this in one exiftool command run, as in our architecture for every exiftool call a microservice is invoked through HTTP with image attached, meaning we could save a lot of time if we do it in one call instead of two.

What I tried so far:
exiftool '-keywords<${keywords@;$_="Merlin Crop 1 2 3 4" if /.?Merlin Crop.*/}' image.jpg - works only if there's a Merlin Crop keyword already in the list.
exiftool '-keywords<${keywords@;$_=undef if /.?Merlin Crop.*/}' -keywords+='"Merlin Crop 1 2 3 4"' image.jpg - looks like only -keywords+= gets executed and old Merlin Crop is still in the list.

Could someone please help me figure out the proper way to combine these two?

Phil Harvey

You were very close, but the += is not necessary since you were already copying the other keywords.  Also, you need the -sep option to break the keywords apart again after they are interpolated in the string.  Try this:

exiftool '-keywords<${keywords@;$_=undef if /.?Merlin Crop.*/}' -keywords='"Merlin Crop 1 2 3 4"' -sep // image.jpg

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Elazul

Perfect! Works like a charm. :)

Thanks for your help, Phil, much appreciated.

PS: exiftool is pretty cool :)

Phil Harvey

...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).