ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: msbc on October 28, 2015, 07:27:21 PM

Title: Keyword Count
Post by: msbc on October 28, 2015, 07:27:21 PM
Is there a way to output the number of items present in a tag, in this case the keywords tag? I can work this out using scripting but would like a way to do it directly with an exiftool command.

As an example, the following command and output on OS X:
10:23 ~/Pictures $ exiftool -keywords MSC_2015-03-03_3299.jpg
Keywords                        : Austurland Region, Iceland, Stokksnes, Vestrahorn, beach, black sand, landscape, mountain, mountains, sand, shoreline, sunset

I'd like to get exiftool to output 12.
Title: Re: Keyword Count
Post by: Phil Harvey on October 28, 2015, 08:45:04 PM
Sure.  This is very sneaky but try this:

exiftool -p "${keywords;$_=(()=/, /g)+1}" FILE

This returns the number of ", " strings in the Keywords plus 1.

- Phil
Title: Re: Keyword Count
Post by: StarGeek on October 29, 2015, 04:45:34 PM
Of course, you have to know your data.  A single "Robert Downey, Jr." or worse, if it's last name first format "Downey, Robert, Jr." and your output is way off.  Changing the separator with -sep is the better option in this case.

I felt like playing around with this and came up with a user defined tag for this.
    KeywordCount => {
        Require => 'Keywords',
        ValueConv => q{
            my @list = ref $val ? @$val : ($val);
            return scalar @list;
        },
    },


Edit:  Ooops, this fails when it comes to single items.  Ignore me!
And the final, very messy single line option. 
exiftool -p "${keywords;my $h =  $$self{VALUE}{Keywords}; $_= scalar @$h }" FILE


Phil, is there a better way to access a list type variable as an array in the advanced formatting?
Title: Re: Keyword Count
Post by: Phil Harvey on October 30, 2015, 07:52:45 AM
Quote from: StarGeek on October 29, 2015, 04:45:34 PM
Phil, is there a better way to access a list type variable as an array in the advanced formatting?

Accessing VALUE directly is not advisable.  Instead, GetValue() should be used:

exiftool -p "${keywords;$_=()=$self->GetValue('Keywords')}" FILE

Here I use the trick of assigning to a temporary array to force list context for the GetValue call.

- Phil