Hi.
Using exiftool 8.38, I'd somehow like to list Keywords seperated by newline, so that I can easily pipe it to a "while" loop in bash. How do I make exiftool seperate the keywords by a \n?
I tried:
exiftool -sep "
" -Keywords file.jpg
exiftool -config ~/ExifTool.config
MacBook-Pro:ports alex$ cat ~/ExifTool.config
%Image::ExifTool::UserDefined = (
'Image::ExifTool::Composite' => {
MyKeywords => {
Require => 'Keywords',
ValueConv => q{
my @list = ref $val eq 'ARRAY' ? @$val : ($val);
$_ = "\n $_" foreach @list;
return \@list;
},
},
MyCaption => {
Require => 'Caption-Abstract',
ValueConv => '"\n $val"',
},
MyHeadline => {
Require => 'Headline',
ValueConv => '"\n $val"',
},
}
);
1; #end
But when I do this, instead of a "\n" (line feed), I get a "."!
MacBook-Pro:Hato Caves alex$ exiftool -config ~/ExifTool.config -MyKeywords *018*g
My Keywords : . Hato Caves, . Curaçao, . Flitterwochen, . ja, nein, doch, . zeile1,.zeile2
MacBook-Pro:Hato Caves alex$ exiftool -sep "
> " -Keywords *018*g
Keywords : Hato Caves.Curaçao.Flitterwochen.ja, nein, doch.zeile1,.zeile2
Am I doing something wrong? FWIW, I'm also attaching the image file.
Thanks,
Alexander
Hi Alexander,
You're not doing anything wrong. The exiftool application filters newlines. Some alternatives are discussed in FAQ number 21 (https://exiftool.org/faq.html#Q21).
- Phil
Hi Phil!
Thanks for the prompt answer! I've read the FAQ 21 (https://exiftool.org/faq.html#Q21), but it mainly deals with how to writer/enter a \n.
So, do I understand the FAQ right, that the answer is: Use JSON or XML output and parse that, if you want to have newlines? :-\
Thanks again,
Alexander
...
Edit:
No, the answer is, use the "-b" switch! :)
$ exiftool -b -Keywords *18*g
Hato Caves
Curaçao
Flitterwochen
ja, nein, doch
zeile1,
zeile2$ exiftool -ver
8.38
Hm, this shows another problem - here, I inserted ONE keyword, which goes over two lines (the last one: zeile1,
zeile2). To overcome this, it would be good, if a \0 (0x00) could be printed (assumption: a \0 will never appear in a keyword).
Solution for this: Use a config file!
%Image::ExifTool::UserDefined = (
'Image::ExifTool::Composite' => {
Keywords0 => {
Require => 'Keywords',
ValueConv => q{
my @list = ref $val eq 'ARRAY' ? @$val : ($val);
$_ = "\0$_" foreach @list;
return \@list;
},
},
}
);
1; #end
But it's still not perfect ;)
$ exiftool -config ~/ExifTool.config -b -Keywords0 *18*g
Hato Caves
Curaçao
Flitterwochen
ja, nein, doch
zeile1,
zeile2
$ exiftool -config ~/ExifTool.config -b -Keywords0 *18*g | hexdump -C
00000000 00 48 61 74 6f 20 43 61 76 65 73 0a 00 43 75 72 |.Hato Caves..Cur|
00000010 61 63 cc a7 61 6f 0a 00 46 6c 69 74 74 65 72 77 |ac..ao..Flitterw|
00000020 6f 63 68 65 6e 0a 00 6a 61 2c 20 6e 65 69 6e 2c |ochen..ja, nein,|
00000030 20 64 6f 63 68 0a 00 7a 65 69 6c 65 31 2c 0a 7a | doch..zeile1,.z|
00000040 65 69 6c 65 32 |eile2|
00000045
As you can see there, exiftool (?) seems to print a linefeed (\n , 0x0a) AFTER the keyword. Can this be solved? I'm not that much of a perl guru *G*
Thanks again,
Alexnader
Hi Alexander,
The -b option inserts a newline between keywords, which seems to be what you wanted originally. Yes this would cause a problem if a keyword contained a newline. If you want only "\0" between keywords, you could use the -b option with this conversion:
ValueConv => q{
my @list = ref $val eq 'ARRAY' ? @$val : ($val);
return join "\0", @list;
},
- Phil
Hi Phil!
Yeah, I originally indeed wanted a newline - but then, while writing the reply, I noticed that just a newline was not such a good seperator :D
With your new valueconv, everything works just perfectly fine now ;)
Thanks again for your help!
Cheers,
Alex