search and replace character in all metadata

Started by Craxo, May 14, 2020, 07:26:39 AM

Previous topic - Next topic

Craxo

Hi,
The functionality of searching in all the tags and executing something was added in version 11.95, many thanks for that!
Now I'm trying to search in all metadata and replace any character that is not: [^[:alnum:][:space:]():.,\-_\"\'%\/+-] and replace it with a space.
this nice command was provided by Phil to search for "<>()" characters:

exiftool -api filter='$$self{Yes}=1 if /[<>()]/' -if '$$self{Yes}' <do something> DIR

However I am failing miserably trying to make the substitution to the matching tags with s/ [^[:alnum:][:space:]():.,\-_\"\'%\/+-] /" "
my question is whether what i want to do is possible and if is there a variable that can be used to make the substitution in this case?

Thank you,
Craxo

StarGeek

I don't believe Perl uses bracket characters classes like those, just backslash character classes.  So replace [:alnum:] and [:space:] with \w and \s.  The underscore is included with the \w.

According to this StackOverflow answer, all you need to escape are the .^$*[\ characters.  And the double quote needs to be escaped if you're using Windows.

Here's the regex that I think covers your list, though I'm not sure if you're trying to include a backslash or not.  If so, add \\.
-api "filter=s/[^\s\w():.\-\"'%\/+]/ /g"
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Craxo

Thank you very much, it does find and replace
exiftool  -api "filter=s/[^\s\w()\-\"\'%\/+]/ /g" file.jpg
but how do I write the substituted version it in the metadata?

Phil Harvey

It would be dangerous to try to filter all metadata like this.  If you did this, it would also apply changes to things like the ThumbnailImage, which wouldn't be a good idea.  Instead, target the specific tags that you want to change:

exiftool -tagsfromfile @ -TAG1 -TAG2 ... -api "filter=s/[^\s\w()\-\"\'%\/+]/ /g" file.jpg

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

Craxo

#4
Thank you very much Phil!
Is there a way excluding only certain metadata?
i cannot seem to do it with -x or -exclude:

exiftool -tagsfromfile @ -x -thumbnailimage -api "filter=s/[^\s\w()\-\"\'%\/+]/ /g" img.jpg

Phil Harvey

The syntax is -x TAG (no dash before the tag name).  Or --TAG accomplishes the same thing.

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