Extract individual items/values from a list-type tag and then alphabetize them

Started by bssmith, September 15, 2023, 11:00:43 PM

Previous topic - Next topic

bssmith

I've successfully configured my desktop search tool (FileLocator Pro) to use ExifTool as a custom extension, so that issuing the following command...

exiftool -Subject -XMP:description -IFD0:Software -XMP:metadatadate -XMP:creatortool -XMP:createdate D:\Desktop\2354.jpg

...produces output in the below format which is then inhaled back into FileLocator Pro as indexable data:

Subject                         : Stanley, Richard, # theBullFamily, MMB, Smith, # photograph, # theMainFamily, ~ The Smiths
Description                     : [2354]
Software                        : Photo Supreme Version 6.7.2.4229
Metadata Date                   : 2022:07:11 05:00:37.906-05:00
Creator Tool                    : Adobe Photoshop Elements 5.0
Create Date                     : 2006:12:02 11:11:48-05:00

This is great! But I want to slice up those eight Subject: metadata list items so that each individual item is on its own line, and to alphabetize those values found within the Subject: output.

The ideal output from the same source image file would instead look like this:

Subject                         : # photograph
Subject                         : # theBullFamily
Subject                         : # theMainFamily
Subject                         : ~ The Smiths
Subject                         : MMB
Subject                         : Richard
Subject                         : Smith
Subject                         : Stanley
Description                     : [2354]
Software                        : Photo Supreme Version 6.7.2.4229
Metadata Date                   : 2022:07:11 05:00:37.906-05:00
Creator Tool                    : Adobe Photoshop Elements 5.0
Create Date                     : 2006:12:02 11:11:48-05:00

I've looked through the forum and the documentation. I see the -sep option - but it looks like this is only for writing out a string of comma-separated metadata tags to a target file? I'm not writing anything here, only reading in the metadata.

FAQ 17 (list-type tags) looks promising but did not reveal an obvious path (that I could recognize, anyway). But the true answer may lie within the API documentation - something which takes the array found within my Subject: tag and carves out each item? GetValue or ExtractInfo, perhaps? But I don't think I see an "alphabetize the output" option within the API.

So my goal is a) split up all of the values within the Subject: tag, and b) alphabetize those values within the Subject: tag. FileLocator Pro is looking for a command-line to invoke, so I'd have to express it that way.

Any pointers? Thanks,

-- Ben

bssmith

Values of list-type tags are read in from the source file individually, and then concatenated in the output string.

Is there a switch or some other approach to change this behavior, so that each value is not concatenated with the others, and is instead output individually?

Might look like this, if -donotconcat existed:

exiftool -donotconcat -Subject D:\Desktop\2354.jpg

This would get me close to the first of my two goals above, by splitting out all the Subject tag values separately.

-- Ben

bssmith

sed to the rescue:

exiftool -s -s -s -Subject D:\Desktop\2354.jpg | sed -e "s/^/Subject                         : /" -e "s/, /\nSubject                         : /g" | sort && exiftool -XMP:description -IFD0:Software -XMP:metadatadate -XMP:creatortool -XMP:createdate D:\Desktop\2354.jpg

Subject                         : # photograph
Subject                         : # theBullFamily
Subject                         : # theMainFamily
Subject                         : ~ The Smiths
Subject                         : MMB
Subject                         : Richard
Subject                         : Smith
Subject                         : Stanley
Description                     : [2354]
Software                        : Photo Supreme Version 6.7.2.4229
Metadata Date                   : 2022:07:11 05:00:37.906-05:00
Creator Tool                    : Adobe Photoshop Elements 5.0
Create Date                     : 2006:12:02 11:11:48-05:00

Still have to confirm this output works as expected with the FileLocator Pro workflow, but ExifTool has done its part of this job.

StarGeek

You might want to take into account that it is possible to have a comma in a keyword.  For example, "Smith, John" and "Doe, Jane".  The output of that would be
C:\>exiftool -G1 -a -s -Subject y:\!temp\Test4.jpg
[XMP-dc]        Subject                        : Doe, Jane, Smith, John
which would be 4 separate keywords using your commands.

You might want to look into the -sep option and use some sort of unique code to separate the tags.  For example
C:\>exiftool -G1 -a -s -sep "--uniqueseparator--" -Subject y:\!temp\Test4.jpg
[XMP-dc]        Subject                         : Doe, Jane--uniqueseparator--Smith, John

But a better option, IMO, would be to output the data using something like the -j (-json) option or the -X (-xmlFormat) option
C:\Programs\My_Stuff>exiftool -G1 -a -json -Subject y:\!temp\Test4.jpg
[{
  "SourceFile": "y:/!temp/Test4.jpg",
  "XMP-dc:Subject": ["Doe, Jane","Smith, John"]
}]

C:\Programs\My_Stuff>exiftool -G1 -a -X -Subject y:\!temp\Test4.jpg
<?xml version='1.0' encoding='UTF-8'?>
<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>

<rdf:Description rdf:about='y:/!temp/Test4.jpg'
  xmlns:et='http://ns.exiftool.org/1.0/' et:toolkit='Image::ExifTool 12.64'
  xmlns:XMP-dc='http://ns.exiftool.org/XMP/XMP-dc/1.0/'>
 <XMP-dc:Subject>
  <rdf:Bag>
   <rdf:li>Doe, Jane</rdf:li>
   <rdf:li>Smith, John</rdf:li>
  </rdf:Bag>
 </XMP-dc:Subject>
</rdf:Description>
</rdf:RDF>
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype