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
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 (https://exiftool.org/config.html) 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
This looks great ... thank you very much.