Extract number from text and calculate

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
vicky_ag
Posts: 6
Joined: 08 Dec 2014 06:16

Extract number from text and calculate

#1 Post by vicky_ag » 08 Dec 2014 06:24

We have a native program which outputs logs in this format:

Code: Select all

Block Compression Ratio                              : 0.005999058
Average Clustering Ratio                             : 1 
Average Fragmentation Quotient                       : 7.236046
Free Space Recovery is Needed                        : No
Estimated Bytes of Recoverable Free Space            : 0

ListFiles:

----- xxx File Information -----

xx File Count:      2

File 1:
   File Name:          xxxx
   File Type:          xxxxx
   File Number:        1 of 2
   File Size:          2,096,016 KB (2,146,320,384 bytes)
   File Opened:        Y

----- zzz File  Information -----

zzz  File Count:      94

File 1:
   File Name:          zzz
   File Type:          zz
   File Number:        1 of 94
   File Size:          2,097,144 KB (2,147,475,456 bytes)
   File Opened:        Y
.........
zzz File Size  Total: 196,666,400 KB (201,386,393,600 bytes)

File Size Grand Total: 199,610,432 KB (204,401,082,368 bytes)


I need to extract two information from this log:
a. Average Fragmentation Quotient
B. The file size total -- this needs to be converted into GB - ie ~ 200GB by the current size. (Mod edit: last line)

Please help :oops:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Extract number from text and calculate

#2 Post by foxidrive » 08 Dec 2014 07:23

Do you need these items in a text file? Or in variables?

Do you have restrictions against using powershell or VBS in a batch file?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Extract number from text and calculate

#3 Post by Squashman » 08 Dec 2014 07:35

Do you need File Size Total or File Size Grand Total?
Why isn't there a File Size Total for the XXX file information?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Extract number from text and calculate

#4 Post by foxidrive » 08 Dec 2014 07:45

Squashman wrote:Do you need File Size Total or File Size Grand Total?

There were some colour codes used in the code block (now removed) which showed that the last line is the total that is needed.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Extract number from text and calculate

#5 Post by dbenham » 08 Dec 2014 08:57

This is a perfect problem for JREPL.BAT :)

Assume the log is named test.log

Code: Select all

jrepl "^Average Fragmentation Quotient\s*:\s*(.*)|^File Size Grand Total:.*\((.*) bytes\)"^
      "'AvgFragQuotient='+$2|'TotalSize='+(Math.round(Number($4.replace(/,/g,''))/1024/1024/1024*10))/10"^
      /jmatch /t "|" /f test.log
--OUTPUT--

Code: Select all

AvgFragQuotient=7.236046
TotalSize=190.4

To get the values in variables:

Code: Select all

@echo off
for /f %%A in (
  'jrepl "^Average Fragmentation Quotient\s*:\s*(.*)|^File Size Grand Total:.*\((.*) bytes\)"^
        "'AvgFragQuotient='+$2|'TotalSize='+(Math.round(Number($4.replace(/,/g,''))/1024/1024/1024*10))/10"^
        /jmatch /t "|" /f test.txt'
) do set "%%A"
echo AvgFragQuotient=%AvgFragQuotient%
echo TotalSize=%TotalSize%


Dave Benham

vicky_ag
Posts: 6
Joined: 08 Dec 2014 06:16

Re: Extract number from text and calculate

#6 Post by vicky_ag » 09 Dec 2014 01:01

Sorry guys for the late reply. I need the output in a text file which will be emailed to a group. There is no restriction on whether VB/Powershell needs to be used.

Edit: @Squashman - there is file size total information for xxx file type but got commented out. I need only the Grand Total.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Extract number from text and calculate

#7 Post by dbenham » 09 Dec 2014 07:06

Did you look at the JREPL solution? It works perfectly well. The output can be directed to a file using /O OUTFILE.TXT, or by redirection. The format of the output can easily be changed.

vicky_ag
Posts: 6
Joined: 08 Dec 2014 06:16

Re: Extract number from text and calculate

#8 Post by vicky_ag » 10 Dec 2014 06:12

dbenham wrote:Did you look at the JREPL solution? It works perfectly well. The output can be directed to a file using /O OUTFILE.TXT, or by redirection. The format of the output can easily be changed.

I tried it but cant seem to get the command working through a bat file. The formatting goes haywire and it says parameters are not correct.
Edit: Got it to work. I was using Fragmentaton (%age) and the % was not working out correctly. Now ran into another issue, the command which is populating the first log seems to be ending the control after it has executed so I am unable to run the jrepl. So it is turing out something like this:

Code: Select all

xyz.bat > test.log --> control ends here and doesn't proceed so jrepl is not executing
jrepl test.log

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Extract number from text and calculate

#9 Post by dbenham » 10 Dec 2014 06:58

If you have XYX.BAT >TEST.LOG in a batch script, then it transfers control to XYZ.BAT, and never returns. If you want to return to your original script, then you need to use CALL XYZ.BAT >TEST.LOG.

I'm not sure what you mean by JREPL TEST.LOG.


Dave Benham

vicky_ag
Posts: 6
Joined: 08 Dec 2014 06:16

Re: Extract number from text and calculate

#10 Post by vicky_ag » 12 Dec 2014 03:33

dbenham wrote:If you have XYX.BAT >TEST.LOG in a batch script, then it transfers control to XYZ.BAT, and never returns. If you want to return to your original script, then you need to use CALL XYZ.BAT >TEST.LOG.

I'm not sure what you mean by JREPL TEST.LOG.


Dave Benham


Thanks it works :oops:
JREPL TEST.LOG was just to indicate that jreply was running after the batch command. :lol:

Post Reply