ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: mike1950r on February 05, 2022, 11:35:04 AM

Title: How can I replace a substring in UserContent
Post by: mike1950r on February 05, 2022, 11:35:04 AM
Hello,

we need to replace a part of the value in UserContent, with another part, or just nothing to remove this part.
Could we please get an idea, how we can do this.

Thanks in advance.

cheers mike
Title: Re: How can I replace a substring in UserContent
Post by: StarGeek on February 05, 2022, 12:09:55 PM
First you need to figure out what tag is "UserContent" (see FAQ #3 (https://exiftool.org/faq.html#Q3)) as exiftool doesn't have a listing for a tag by that name.

Once you figure out the actual tag, then to remove it the command would be
exiftool -TAG= /path/to/files/

To replace a part, you would use
exiftool "-TAG<${TAG;s/SEARCH/REPLACE/g}" /path/to/files/
Title: Re: How can I replace a substring in UserContent
Post by: mike1950r on February 05, 2022, 12:59:18 PM
Hello,

thanks StarGeek.
This works fine exept one Character '|'
This character is not replaced.

Is this a special perl character?
What can i do?

I have to replace "| and a word".

Thanks for assistance.

cheers mike
Title: Re: How can I replace a substring in UserContent
Post by: StarGeek on February 05, 2022, 01:05:31 PM
The pipe character is a special character in regex.  If you need to use any of these characters
. ^ $ * + - ? ( ) [ ] { } \ | — /
then you need to escape it with a backslash. So "| and a word" would become "\| and a word".
Title: Re: How can I replace a substring in UserContent
Post by: mike1950r on February 05, 2022, 01:36:19 PM
Thank you very much, StarGeek,

This is working fine.

So it's possible to append a value with following conditions:
1) append only if Tag already exists
2) append only if value to append not already exists.

Very flexible indeed.

Cheers mike
Title: Re: How can I replace a substring in UserContent
Post by: StarGeek on February 05, 2022, 03:34:49 PM
Quote from: mike1950r on February 05, 2022, 01:36:19 PM
1) append only if Tag already exists

exiftool "-TAG<$TAG more text" /path/to/files/
Any file that doesn't already have the TAG will fail with a "No writable tags set" error, but you can ignore that

Quote2) append only if value to append not already exists.

A bit more complicated and you have to make sure it's unique, but you can use the -if option (https://exiftool.org/exiftool_pod.html#if-NUM-EXPR)
exiftool -if "not $TAG=~/more text/" "-TAG<$TAG more text" /path/to/files/

An example of what I mean by unique.  If the above command found a file with "more texture", then the "more text" would match and it would be skipped.  This can be compensated by using a more exacting regular expression.
Title: Re: How can I replace a substring in UserContent
Post by: mike1950r on February 06, 2022, 06:59:15 AM
Hi StarGeek,

we use following with success:

exiftool -if $UserComment -UserComment<${UserComment;s/APPENDTEXT//g} APPENDTEXT ...

1) Append only if UserComment tag exists
2) Append only if APPENDTEXT not already exists

Works exellent.

cheers mike
Title: Re: How can I replace a substring in UserContent
Post by: mike1950r on February 06, 2022, 07:52:29 AM
Hi StarGeek,

does your command: -if "not $TAG=~/more text/" "
mean more text is not a part in the tag value, or does it mean it is not the tag value?

Also how can i use two if conditions?
-if $UserComment
and
-if "not $TAG=~/more text/" "

Cheers mike
Title: Re: How can I replace a substring in UserContent
Post by: Phil Harvey on February 06, 2022, 07:55:55 AM
You could do it like this:

-if "$usercomment and not $usercomment=~/more text/"

- Phil
Title: Re: How can I replace a substring in UserContent
Post by: mike1950r on February 06, 2022, 12:11:24 PM
Thanks Lot Phil,

Everything is really working fantasticly.
You are so helpful here in the forum,
also with the perl questions, which enables using the capabilities of exftool a lot.

This also works fine for removing a part of the tag value:
-if $usercomment and $usercomment=~/REMOVETEXT/ -UserComment<${UserComment;s/REMOVETEXT//g}

Thanks lot again.

Cheers mike