Searching for and removing the value of one tag within another tag?

Started by ne17, February 11, 2022, 05:45:20 PM

Previous topic - Next topic

ne17

Using Exiftool 12.39 on Win 10.

I have a lot of geotagged images - I used to include the City name at the start of the Location tag sometimes with more detail.

I would now like to remove the City name (and the separating space) from the start of Location tag if the value matches.

After lots of googling and experiments (the test and dealing with $City=$Location is straightforward) but I'm failing to get the  match / replace to work and have reached
exiftool -if "($MWG:location ne '') && ($MWG:Location ge $MWG:City)"                                                                                                                               
                  "-MWG:Location<${MWG:location;m/(\$MWG:City )(.*)/;$_=$2}"  -use mwg -ext DNG .

This gives the warning Advanced formatting expression returned undef

Is use of a tag within a regex possible?

I've struggled to work out how to use =~ within an exiftool command or if that would help

ne17

ok spotting some Perl in another post gave my an idea which works, but needs bulletproofing against my data...

exiftool "-MWG:Location<${location;my $temp= substr ($self->GetValue('Location') , length ($self->GetValue('City')) +1 ); $_=$temp}"   -use mwg -ext DNG .

Is there a better way?

StarGeek

You should be able to replace
$self->GetValue('Location')
with
$_
which is the default variable in Perl.  In the context of exiftool, it holds the contents of the tag it appears in.  And $self->GetValue() would be the way to get the value of a different tag.
"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

ne17

ok - teaching myself Perl using exiftool on a windows CMD screen isn't the fastest, but - the code at the bottom works (except when location has 1 space after the city name - more thinking needed to empty location in that case.

I got strange results when I tried $_ instead of what is now $temploc.

My eureka moment is this approach can remove lots of the -if ... -execute pairs I have in my regular batches - which run slowly because of the multiple reads and writes of the image files.
The only downside of not wanting to include a -if $MWG:Location is   

Warning: [minor] Tag 'MWG:Location' not defined -  - ./152 G 29.dng
Warning: No writable tags set from ./152 G 29.dng

for image files without $MWG:Location

This caters for all of the cases in my data (except for $location having an extra space to $City)

Any suggestions?


exiftool "-MWG:Location<${MWG:Location;      { my $temploc=$self->GetValue('MWG:Location');
                                                                        my $tempcity=$self->GetValue('MWG:City');
                                                                        my $temp= undef;
                                                                        if( $temploc && $tempcity )                                                                                                                                 
                                                                          { if( $temploc eq $tempcity)  { $_ = ''; }                                                                                                                 
                                                                             elsif( index($temploc, $tempcity ) == 0)  { $_ = substr($temploc, length ($tempcity) +1 ); }                                                             
                                                                             else { $_ = $temploc; }                                                                                                                                 
                                                                          }                                                                                                                                                       
                                                                          elsif( $temploc ) {$_ = $temploc; }                                                                                                                       
                                                                          else {$_ = ''; }                                                                                                                                         
                                                                         } 
                                             }"
    -fast2 -use mwg -ext DNG -charset filename=utf8 -charset exif=utf8 .

ne17

Sorry for replying to my own posts but I've fixed the extra space problem - I'd removed .' ' to identify an error and forgotten to put it back...

final code

exiftool "-MWG:Location<${MWG:Location;      { my $temploc=$self->GetValue('MWG:Location');
                                                                        my $tempcity=$self->GetValue('MWG:City');
                                                                        my $temp= undef;
                                                                        if( $temploc && $tempcity )                                                                                                                                 
                                                                          { if( $temploc eq $tempcity)  { $_ = ''; }                                                                                                                 
                                                                             elsif( index($temploc, $tempcity.' ' ) == 0)  { $_ = substr($temploc, length ($tempcity) +1 ); }                                                             
                                                                             else { $_ = $temploc; }                                                                                                                                 
                                                                          }                                                                                                                                                       
                                                                          elsif( $temploc ) {$_ = $temploc; }                                                                                                                       
                                                                          else {$_ = ''; }                                                                                                                                         
                                                                         }
                                             }"
    -fast2 -use mwg -ext DNG -charset filename=utf8 -charset exif=utf8 .