0

I have a large folder tree, and would like to make a script to open up gThumb or some other image viewer based on a keyword search. exiftool is already installed on my system. and the command

exiftool -a -r -G1 -s $FILE 

prints the data out in format

[ExifTool]      ExifToolVersion                 : 8.60
[System]        FileName                        : 4-4_wlax_0127.jpg
[System]        Directory                       : .
[System]        FileSize                        : 6.3 MB
[System]        FileModifyDate                  : 2012:04:05 13:57:54-04:00
[System]        FilePermissions                 : rwxrwxrwx

Currently,

exiftool -a -r -G1 -s . | grep Keywords | grep "$1*" 

prints out the Keyword row of the metadata as

[IPTC]          Keywords                        : 2012, Womens Lax, ..., ...

where $1 is the keyword search value. Right now, my solution is to use two loops, each executing an exiftool call for a different grep value. How can I excute a single exiftool call that for matching keywords, the associated filename is passed to a list.

EDIT- Clarification:

If a match exits to the Keyword field, I want to print out the FileName row. However, Keyword exists ~20 lines below FileName in the text output.

Jason
  • 11,263
  • 21
  • 87
  • 181

3 Answers3

2

Just use , rather than all those pipes!

% exiftool -a -r -G1 -s . | awk -v search='your search string here' '
    $2 ~ /FileName/ { filename = $0 }
    $2 ~ /Keywords/ && $0 ~ search { print filename }
'

This assumes that Filename always appears before Keywords for each file processed. If you have multiple strings you wish to search for, just pipe-separate them (it's a regular expression!):

% exiftool -a -r -G1 -s . | awk -v search='first|second' '
    $2 ~ /FileName/ { filename = $0 }
    $2 ~ /Keywords/ && $0 ~ search { print filename }
'

If you just want the filenames (and not the entire row), perhaps this is a neater solution:

% exiftool -a -r -G1 -s . | awk -F': ' -v search='first|second|third' '
    $1 ~ /FileName/ { filename = $2 }
    $1 ~ /Keywords/ && $2 ~ search { print filename }
'

You can also do all of your text processing in the same program, of course.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
2

Use the exiftool -if option:

exiftool -filename -s3 -if '$keywords=~/some keyword/' .

Phil Harvey
  • 390
  • 2
  • 4
  • Thanks for responding, I completely missed that option. Piping the output to `grep Filename | awk '{print $2}'` did the trick along with adding the `-r` flag. – Jason Feb 14 '13 at 16:06
  • 2
    But with -filename and -s3 too, you shouldn't need to use awk. The ExifTool output options are so flexible there should never be any need to use awk at all. – Phil Harvey Feb 14 '13 at 16:33
  • don't get your cmd totally.. what is the sense of the s13? and how it is possible to process the selected files to open it in a another application? Your answer looks like that everything should possible :) Thanks for your help! – rokdd May 19 '13 at 19:07
1

If I understand your question correctly you have multiple keywords your want to filter for. Use egrep and the |-operator:

exiftool -a -r -G1 -s . | grep Keywords | egrep "$1|$2" 

whereas $1 is the first keyword and $2 is the second one. egrep can be replace with grep -E as egrep is deprecated.

EDIT (after question has actually changed): You should have made a new question.

You could make a loop over all files and if a file matches one of your keywords you use it. E.g.:

for n in `ls`; do
    c=`exiftool -a -G1 -s $n | grep Keywords | egrep "$1|$2" | wc -l`
    if [ $c -gt 0 ]; then
         echo $n has matched
    fi
done
Patrick B.
  • 11,773
  • 8
  • 58
  • 101
  • Almost. The output is on multiple lines, where Keyword is about 20 rows below Filename, which is line 2 of output. If there is a match to the Keyword, I want to print the FileName. – Jason Feb 13 '13 at 21:13