ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: bs1925 on July 17, 2016, 10:46:28 AM

Title: character replacing during output
Post by: bs1925 on July 17, 2016, 10:46:28 AM
Hi

for isolating hierachical keywords I use -xmp-lr:HierarchicalSubject to write to a text file. Afterwards I search and replace "," by ";" and "|" by " > ".

How can I do this during running exiftool and already write the replaced characters to the text file?

regards
Bernd
Title: Re: character replacing during output
Post by: StarGeek on July 17, 2016, 03:39:18 PM
It sorta depends upon what you're doing with the text file.  If you're just reading it, then you can do something like this:
exiftool -p "$filename ${HierarchicalSubject;tr/,\|/;>/}" >output.txt FilesOrDir
That will output a list of filenames and their HierarchicalSubject. Not highly formatted, but quick and simple.

If you need to process the output with a script or something similar, then you're better off making a user defined tag, something like this:
MyHS=> {
Require => 'HierarchicalSubject',
ValueConv => q{
my $hs = join('; ',(ref $val ? @$val : ($val)));
$hs =~ tr/\|/>/;
return $hs;
},         
},


Example output (HS is my shortcut for HierarchicalSubject, easier than typing the whole thing out every time):
c:/ exiftool -hs -myhs Z:\Pictures\Camera_Pictures\San_Diego_Zoo\2010-05-15\2010-05-14_12.58.08.Jpg
Hierarchical Subject            : Animals|Marsupials|Koala, Settings|San Diego Zoo
My HS                           : Animals>Marsupials>Koala; Settings>San Diego Zoo

Title: Re: character replacing during output
Post by: bs1925 on July 18, 2016, 02:58:35 AM
wow - the first quick and simple command works perfect! THX

one additional question - I would have to replace the "|" with " > " - there is a space character before and after the ">" - how does this work?

regards
Bernd
Title: Re: character replacing during output
Post by: Hayo Baan on July 18, 2016, 03:11:04 AM
Instead of the simple tr/,\|/;>/ use the following tr/,/;/; s/\|/ > /g

tr/,/;/ will replace all occurrences of , with ;, s/\|/ > /g will substitute all | with " > ".
Title: Re: character replacing during output
Post by: bs1925 on July 18, 2016, 05:31:42 AM
works perfect! THX

I only need a simple list with filename and the hierarchical keywords.
last question: how can I adopt the command to print only the filename without any extensions?

ie exiftool -p "$filename ${HierarchicalSubject;tr/,/;/; s/\|/ > /g}" >output.txt key.jpg

regards
Bernd
Title: Re: character replacing during output
Post by: Hayo Baan on July 18, 2016, 11:59:29 AM
You could try something like this exiftool -p "${filename;s/\.[^.]+//} ${HierarchicalSubject;tr/,/;/; s/\|/ > /g}" >output.txt key.jpg
Title: Re: character replacing during output
Post by: bs1925 on July 20, 2016, 11:44:33 AM
I tried to understand the used perl commands - got it, more or less ;-(

so, inserting "$/" within -p represents a newline - but how can I seperate the two fields with a tabulator?
I cannot find this option.

regards
Bernd
Title: Re: character replacing during output
Post by: Phil Harvey on July 20, 2016, 12:33:50 PM
Hi Bernd,

You can put a tab directly in the -p argument -- it will come straight through in the output.  The only problem would be if your shell intercepts the tab character, which is quite likely as it turns out.  I don't have much time right now to think of a way around this, but I'll come back later with a solution.

- Phil

Edit:  On a Mac, a tab is entered by pressing CTRL-V then TAB.  I don't know about Windows.