Sorting Exiftool text output alphabetically by group and tag name

Started by aperturemode, September 20, 2023, 09:43:25 PM

Previous topic - Next topic

aperturemode

Consider this write command applied to all the JPG files from Image 100 to 199:

exiftool -w txt -sort -G0:1 -a -s IMG_01??.JPG
I want the resulting text file to be sorted alphabetically by Group 0, Group 1, and then the tag time. So a simplified output, sorted correctly, would look like:

[Composite]     DriveMode                       : Continuous Shooting
[Composite]     FOV                             : 24.1 deg
[Composite]     FlashType                       : External
[EXIF:ExifIFD]  ApertureValue                   : 5.6
[EXIF:ExifIFD]  ColorSpace                      : sRGB
[EXIF:ExifIFD]  WhiteBalance                    : Auto
[EXIF:IFD0]     ResolutionUnit                  : inches
[EXIF:IFD0]     XResolution                     : 96
[EXIF:IFD0]     YCbCrPositioning                : Centered
[MakerNotes:Canon] AEBBracketValue              : 0
[MakerNotes:Canon] AFAreaHeight                 : 151
[MakerNotes:Canon] ZoomTargetWidth              : 3072

The 's-sort' argument, if I'm using it correctly, doesn't do that. Is there another possibility?

I'm using Mac and can take the resulting text file in Terminal and use the sort command. But I have to sort file byy file as I'm not sure how to sort a batch of files at once. So, the command I use is:

sort -o IMG_0001.txt IMG_0001.txt

StarGeek

The -sort option only sorts by tag descriptions or names.

The Mac should have the sort command, but that doesn't seem to be able to process files in batch by itself.  You might look at this SuperUser question.  There's a short script there that will run sort in batch.

I don't use batch scripts, but I think this might work to only run on text files.  Also, the original created the sorted files in a subdirectory, but I'm pretty sure that using sort filename.txt will sort in place
for file in *.txt;
do
   printf 'Processing %s\n' "$file"
   [ -f "$file" ] && sort -u "$file"
done

Hopefully, someone with more experience will check this and fix any problems.
"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

aperturemode

Thanks, StarGeek. You got me on the right path.

I got it working with this:

for file in *.txt;
do
printf 'Processing %s\n' "$file";
sort -o $file $file;
done

It's odd, but sort in Mac isn't set up to sort in place unless you specifically state the output file. And you need to use the -o argument to print the output to the text file instead of 'standard output' whatever that is. I'm not sure what -u is in your system but on the Mac command it returns only unique keys, so I dropped that arugument so that duplicate keys are also output.

One thing I wasn't able to get working was to save the output text file in a subdirected, such as ./sortedtextfiles/IMG_0001.txt I couldn't figure out the write syntax to add the directory name to the output file.

But I was able to make a script file (plain text, not rtf), for example named sortexiftool.sh. You can copy the above into a plain text file, save it, use chmod to make it an executable and run it from Terminal. You may also need to add '#!/bin/zsh' as the first line in the text file or #!/bin/bash' if you are using a bash shell instead of zshell.

chmod 700 sortexiftool.sh
./sortexiftool.sh



StarGeek

Quote from: aperturemode on September 21, 2023, 04:43:07 PMIt's odd, but sort in Mac isn't set up to sort in place unless you specifically state the output file. And you need to use the -o argument to print the output to the text file instead of 'standard output' whatever that is. I'm not sure what -u is in your system but on the Mac command it returns only unique keys, so I dropped that arugument so that duplicate keys are also output.

I was only going off of what that link I gave said.  I'm on Windows and while I do have a port of the sort (and other Unix commands), I don't use it often to know the options.  And the -u is mentioned in the link.

QuoteOne thing I wasn't able to get working was to save the output text file in a subdirected, such as ./sortedtextfiles/IMG_0001.txt I couldn't figure out the write syntax to add the directory name to the output file.

The -w (-TextOut) option format string needs to be expanded.  Try
exiftool -w %dsortedtextfiles/%f.txt -sort -G0:1 -a -s IMG_01??.JPG

The %d stands for the directory of the jpeg, including the trailing slash.  Then your subdirec tory of sortedtextfiles/ (slash needed here), followed %f which is the base name of the jpeg, followed by the .txt extension.
"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

aperturemode

Thanks again. I was sort of wondering what the 'd' in %d stood for.