How to replace string-part in [XMP-dc] Description and [IPTC] Caption-Abstract?

Started by snahl, April 13, 2011, 08:36:31 PM

Previous topic - Next topic

snahl

Hello Phil and fellow-'ExifToolers',
hope somebody can help me on this one.

In a given text-string in the 2 fields [XMP-dc] Description and [IPTC] Caption-Abstract, I'd like to replace a portion of text like 'xyz' with a coma (,).
I can read the whole string and replace it with another complete string using 2 commands per tag name. But unfortunately I have no idea how to replace only a part of a given string in 2 tags per image and possibly in one command-line. The files affected are all in the same folder.

Looking forward to receiving a solution,
thanks, Hans.
Dig-IT-all

Phil Harvey

Hi Hans,

This can be done with user-defined tags.  Here are a couple of examples of how to do this:

example 1

example 2

- 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 ($).

snahl

Thanks for sending the examples.
I widened my search and came across this post from 'pixelpicker':
https://exiftool.org/forum/index.php/topic,2341.msg10328.html#msg10328
Thanks to this post I was able to pretty much resolve my search and replace issue.

But how about if that string to be replaced also contains a / and a < and a >?
To be specific I also need to search for and replace a <br /> (my DAM-sw put that HTML-Tag in there).


  %Image::ExifTool::UserDefined = (     
    'Image::ExifTool::Composite' => {
        MyDescription => {
          Require => 'Description',
          ValueConv => q{
            my @list = ref $val ? @$val : ($val);
            my $changed;
            s/<br />/,/g and $changed = 1 foreach @list;
            return $changed ? \@list : undef;
          },
        },
      },
  );

Command line:
exiftool -config .ExifTool_config "-Description<myDescription" *.jpg


The / inside the <br /> is causing this search not to function as anticipated.
This / needs to be reformatted, so that it is recognized as part of the string and not as part of the command itself.
Any hint leading to the solution is highly appreciated.

Thanks, Hans.
Dig-IT-all

Phil Harvey

Hi Hans,

There are various ways to solve this.  Either escape the offending character with a backslash:

    s/<br \/>/,/g

or use other delimiters for the substitution expression:

    s{<br />}{,}g

Also, you don't need to worry about a list of values since Description isn't a List-type tag, so this will do:

        MyDescription => {
          Require => 'Description',
          ValueConv => '$val =~ s{<br />}{,}g ? $val : undef',
        },


- 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 ($).

snahl

Phil,
many thanks, this works great, just as anticipated.
Greetings.
Dig-IT-all