iterating over list-type tag to find duplicates

Started by Storhaug, May 16, 2012, 01:55:55 PM

Previous topic - Next topic

Storhaug

In a bash script, I want to check for duplicate names in the list-type tag XMP-iptcExt:PersonInImage. Has anyone already invented this wheel and better than below?

Moreover, I am very happy to learn about best practices on how to iterate over list-type tags in bash.

Currently, I am iterating over the list of names and add them into an associative array. If an entry already exists then the name is a duplicate.

#!/bin/bash

declare -A personInImageHash
IFS='#'
personInImage=$(exiftool -s -s -s -E -sep "${IFS}" -XMP-iptcExt:PersonInImage "${1}")

for personName in $personInImage; do
  if [ "${personInImageHash[${personName}]}" ]; then
    echo "Duplicate name: $personName"
  else
    personInImageHash[${personName}]=$personName
  fi
done

Phil Harvey

Wow, your bash scripting talents are superior to mine.

I would have done this with a user-defined ExifTool tag.  Here is a config file to do this:

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        MyDups => {
            Require => 'XMP-iptcExt:PersonInImage',
            ValueConv => q{
                return undef unless ref $val eq 'ARRAY';
                my ($v, %seen, @dups);
                foreach $v (@$val) {
                    push @dups, $v if $seen{lc $v};
                    $seen{lc $v} = 1;
                }
                return \@dups;
            },
        },
    },
);
# end


The MyDups tag will return the duplicate PersonInImage names (or it won't exist if there are no duplicates).  Here I have used a case-insensitive comparison.

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

Storhaug