Modify the contents of a tag (by replacing a word).

Started by philbond87, July 17, 2021, 11:11:46 PM

Previous topic - Next topic

philbond87

I'm trying to figure out how I can change the value of a tag – for a large number of image files – with the current contents of that tag, but with a word changed.

For example, if the contents of the tag -CreatorAddress was "123 Main St"
How would I change it so that the tag's contents were "123 Main Street"

I would like to change all of the files in the directory (or even texted directories, using the -r tag) so that all of the instances of "St" in that tag were changed to "Street"


Thank you!

Alan Clifford

Quote from: philbond87 on July 17, 2021, 11:11:46 PM
so that all of the instances of "St" in that tag were changed to "Street"


Be careful!

Stone St

Last St.


Phil Harvey

exiftool "-creatoraddress<${creatoraddress;s/\bSt\b/Street/}" DIR

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

philbond87

Thank you Phil. That did it.
My attempts weren't working as I didn't as I didn't use (or understand) the use of /b.

StarGeek

Quote from: philbond87 on July 18, 2021, 06:54:32 AM
My attempts weren't working as I didn't as I didn't use (or understand) the use of /b.

The \b is a zero length anchor in regex.  Copying details from regular-expressions.info
QuoteThere are three different positions that qualify as word boundaries:

    Before the first character in the string, if the first character is a word character.
    After the last character in the string, if the last character is a word character.
    Between two characters in the string, where one is a word character and the other is not a word character.

Regular Expressions are a complex subject.  The site I linked is where I learned how to use them.  And RegEx101 is a good site to test out regex and it even gives a step by step breakdown of the expression.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

philbond87

Thank you both.

How would I add a conditional check to only do the substitution if a specific string was found in that tag?

Forever grateful,
Phil

StarGeek

You could use
exiftool -if "$creatoraddress=~m/\bSt\b/" "-creatoraddress<${creatoraddress;s/\bSt\b/Street/}" /path/to/files/

Swap double quotes for single quotes on Mac/Linux/Windows PS
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

philbond87

Thanks @stargeek.

My problem is that I'm trying to put multiple conditionals in one command, as in if it's ST then change it to Street, or if it's AVE then change it to Avenue, etc.

philbond87

I thought perhaps I could just run the conditional statements sequentially however that doesn't seem to work.

StarGeek

Try this
exiftool -if "$creatoraddress=~m/\b(St|Ave)\b/i" "-creatoraddress<${creatoraddress;s/\bSt\b/Street/i;s/\bAVE\b/Avenue/i}" /path/to/files/

The -if option now checks against "st" or "Ave" by itself and is now case insensitive.  Then for the replacement, each replacement is separated by a semicolon, as well as being case insensitive.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

philbond87

Beautiful – I really appreciate the assistance.

Phil Harvey

This can also be done without the -if statement:

exiftool "-creatoraddress<${creatoraddress;s/\bSt\b/Street/i or s/\bAVE\b/Avenue/i or $_=undef}" /path/to/files/

I don't think there is any real advantage to doing it this way here, but I put it out there because it may be useful in some situations.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

StarGeek

The biggest disadvantage to doing it without the -if option is that it will throw Warning: No writable tags for any file that isn't affected and that tends to worry some users.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Phil Harvey

...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).