ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: yevev35775 on October 17, 2021, 09:13:39 AM

Title: Remove last (empty) line from text-file with outputted Exif-data
Post by: yevev35775 on October 17, 2021, 09:13:39 AM
I use this command (as an example);

exiftool -w! yaml -p "app__ImageWidth: $ImageWidth$/app__ImageHeight: $ImageHeight" -r -ext jpg "img"

This outputs the following into a .yaml file;

app__ImageWidth: 3264
app__ImageHeight: 2448



As you can see in the output above and image below a blank line (-break) is included at the end of the file.

(https://i.ibb.co/8DWchg7/image.png)

Is there any way to exclude / skip that last linebreak?

.Yaml files are rendered line by line on my server and with about 10.000 of those files, it has to skip the last line also 10.000 times :-)

I use Windows 10 / tried to fiddle with the -L option and the output options, but the empty last line is always there.
Title: Re: Remove last (empty) line from text-file with outputted Exif-data
Post by: Phil Harvey on October 17, 2021, 10:42:05 AM
Interesting.  I would say that this isn't a blank line, but that the last line is terminated by a linefeed.  This is also the way many text readers process the file -- they would read two lines.  I don't recall ever seeing one that would treat this as 3 lines.  (Notice that you needed to insert 2 newlines at the end of the code block in your post.)  In fact, it is a problem for some systems if the last line doesn't end in a linefeed -- it makes it look as if the file was truncated.

Having said this, you can do what you want by creating a .fmt file like this without a newline at the end:

app__ImageWidth: $ImageWidth
app__ImageHeight: $ImageHeight


and using the -p FMTFILE form of the option.

- Phil
Title: Re: Remove last (empty) line from text-file with outputted Exif-data
Post by: yevev35775 on October 17, 2021, 12:32:18 PM
Yeah, it's not quite a new line, but indeed some kind of carriage return.

I am always struggling with those things in Javascript (/r/n), databases, PHP (chr(10) / chr(13), PHP_EOL, etc...

The .yaml files are processed by .php and do not render the last line (since it's empty), but it does process them (which is useless in my case).




The .FTM solution works very well, but leads into another problem.

This is how it works now;

command line

exiftool -w! yaml -p "template.fmt" -r -ext jpg "img"

template.fmt

app__FileName: $FileName
app__CreateDate: $CreateDate
app__Aperture: $Aperture
app__ShutterSpeed: $ShutterSpeed
app__ImageWidth: $ImageWidth
app__ImageHeight: $ImageHeight
app__Device: $Make / $Model


output.yaml

app__FileName: IMG_20191001_093645.jpg
app__CreateDate: 2019:10:01 09:36:45
app__Aperture: 1.8
app__ShutterSpeed: 1/125
app__ImageWidth: 3264
app__ImageHeight: 2448
app__Device: Xiaomi / Mi 9 SE


As you can see, the last line is now removed;

(https://i.ibb.co/BB4JrcC/image.png)

The .yaml file is now processed by .php, converted into an array and spit out in a table like this;

(https://i.ibb.co/3pcCSbf/image.png)




The issue now is that I also want to inject a custom line in the .yaml from the command line (so not set in the .fmt file itself).

Is that possible?

I want add this line before the output in the .yaml file (so the first line in that file, before the .fmt kicks in);

app__FileType: p => photo

I tried things like this, but that didn't work out;

exiftool -w! yaml "app__FileType: p => photo" -p "template.fmt" -r -ext jpg "img"


Title: Re: Remove last (empty) line from text-file with outputted Exif-data
Post by: Phil Harvey on October 17, 2021, 12:44:15 PM
Quote from: yevev35775 on October 17, 2021, 12:32:18 PM
As you can see, the last line is now removed;

I don't understand.  There are 7 lines in your format file, and 7 lines in the output.

QuoteThe issue now is that I also want to inject a custom line in the .yaml from the command line (so not set in the .fmt file itself).

Is that possible?

Yes.  See the -userParam option.

- Phil
Title: Re: Remove last (empty) line from text-file with outputted Exif-data
Post by: StarGeek on October 17, 2021, 01:20:12 PM
Is app__FileType: p => photo a static string or is it some variable you want to pass from the command line?

If it's a static string, just place at the location you want it to appear in the FMT file.

If it's a variable passed from the command line, use the -UserParam option (https://exiftool.org/exiftool_pod.html#userParam-PARAM-VAL) as Phil says
exiftool -userparam MyVar="text from cmd line" -w! yaml -p "template.fmt" -r -ext jpg "img"
And then you would use $MyVar where you want in the FMT file.
Title: Re: Remove last (empty) line from text-file with outputted Exif-data
Post by: yevev35775 on October 17, 2021, 01:25:47 PM
Quote from: Phil Harvey on October 17, 2021, 12:44:15 PM
Quote from: yevev35775 on October 17, 2021, 12:32:18 PM
As you can see, the last line is now removed;
I don't understand.  There are 7 lines in your format file, and 7 lines in the output.

Sorry, I meant "the carriage return is now gone" (which is good!).

.YAML considers the final carriage return as a new line, so that's why I mentioned it as "a new line" (but it's a carriage return).

Before the situation the file had 7 lines and a trailing carriage return, so .yaml would process 8 "lines".

Now it has 7 lines and no carriage return, so .yaml perfectly fetches 7 entries.

As for the -userParam option - I tried several things from the guide, but didn't work it out.

To keep it simple, this is what I want to achieve;

template.fmt

foo: $bar

output.yaml


hello: world
foo: whatever


And "hello : world" must be "injected" at top of the outputted .yaml, before the .fmt is active;

command

exiftool -w! yaml -p -userparam "hello: world" "template.fmt" -r -ext jpg "img"

(Where "hello : world" is always the same => it's just a static entry, but I just don't want to add it to the .fmt file for some reasons).
Title: Re: Remove last (empty) line from text-file with outputted Exif-data
Post by: yevev35775 on October 17, 2021, 01:35:41 PM
Quote from: StarGeek on October 17, 2021, 01:20:12 PM
Is app__FileType: p => photo a static string or is it some variable you want to pass from the command line?

If it's a static string, just place at the location you want it to appear in the FMT file.

If it's a variable passed from the command line, use the -UserParam option (https://exiftool.org/exiftool_pod.html#userParam-PARAM-VAL) as Phil says
exiftool -userparam MyVar="text from cmd line" -w! yaml -p "template.fmt" -r -ext jpg "img"
And then you would use $MyVar where you want in the FMT file.

Thank you! It's working!

The first line should be a static line, but I didn't want to "hard code" it into the .fmt file for reasons.

With your help, I can now even make a dynamic entry of it;

command

exiftool -userparam FileType="scanned document" -w! yaml -p "template.fmt" -r -ext jpg "img"

template.fmt

app__FileType: $FileType
app__FileName: $FileName
app__CreateDate: $CreateDate
app__Aperture: $Aperture
app__ShutterSpeed: $ShutterSpeed
app__ImageWidth: $ImageWidth
app__ImageHeight: $ImageHeight
app__Device: $Make / $Model


output.yaml

app__FileType: scanned document
app__FileName: IMG_20191001_093645.jpg
app__CreateDate: 2019:10:01 09:36:45
app__Aperture: 1.8
app__ShutterSpeed: 1/125
app__ImageWidth: 3264
app__ImageHeight: 2448
app__Device: Xiaomi / Mi 9 SE


As you can see the parameter app__FileType is now set from the command line to scanned document, which is outputted in the .yaml file

(https://i.ibb.co/1zhYszs/image.png)

So the EXIF is taken from a .JPG, formatted by .FMT, outputted to .YAML, parsed by .PHP and rendered as .MD (markdown) into .HTML

All power to the open formats :-)

I have created a own framework that uses (where possible) Human Readable Formats... that's why I wanted to get rid of the deprecated carriage return and following this flow.