Massaging Textual Data

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Massaging Textual Data

#16 Post by Samir » 31 Oct 2015 14:23

Aacini wrote:
Samir wrote:In my original thinking, an array is the other data structure I was thinking to store the product/moniker table in and then somehow retrieve that. But updating the table if anything changes is still a chore--and it's a chore no matter what format we store the table in since it would have to be updated. Hence why I chose the route to just hard code it.

(Anytime a product would have to be updated, so would the moniker. In hard-coding, just the moniker would have to be updated. This also gives some flexibility as a small product change like Ice to Ice Bags would not require that the script be updated so long as it still corresponded with the same moniker.)

I am afraid I don't understand your point. If the table needs to be updated it requires just one change: if it is in the program, change the program; if the table is in a data file, just change the data file. For example, this could be the table:

Code: Select all

Unl-Self=Fuel:87
Ul-Plus-Self=Fuel:89
Supreme-Self=Fuel:93
Diesel-Self=Fuel:Diesel
Candy=Non-Fuel:Candy
Coke Product=Non-Fuel:Coke
etc...

... and this table could be loaded into "moniker" array this way:

Code: Select all

for /F "tokens=1,2 delims==" %%b in (monikerTable.txt) do (
   set "moniker[%%b]=%%c"
)

This method have the advantage that you may use the table for other things; for example, you may sort it alphabetically, or add prizes to it, etc...

Antonio
I see what your're saying now. It's more object oriented when things like the array are stored elsewhere.

The problem is that I've already hard-coded most of the other elements in the other batches that work with this one. I'll still be updating everywhere if anything has to be changed.

And when that change comes, it's going to be so dramatic that I'll probably have to scrap all this work for a completely new system. But when it comes time to code for that new system, I'll try to use what you're saying for sure. 8)

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Massaging Textual Data

#17 Post by Samir » 31 Oct 2015 14:41

aGerman wrote:OK, I guess that could be what you have in mind

Code: Select all

@echo off &setlocal
set "txt_in=test.txt"
set "txt_out=test2.txt"

setlocal EnableDelayedExpansion
<"!txt_in!" >"!txt_out!" (
  for %%i in (
    "Fuel:87"
I have a couple of questions here.

I can make a file for input via

Code: Select all

MORE /S +128 SOURCEREPORTFILE > TEST.TXT
But is there a way to just nest the command instead of using a file? I've tried this without any success:

Code: Select all

<"MORE /S +128 SOURCEREPORTFILE" >"!txt_out!" (
Second question is that I have an existing file called TOTLSOLD.TXT with output from another batch. I would like the data from this batch to append to it. My thought is that

Code: Select all

>"!txt_out!"
could be changed to this to achieve it

Code: Select all

>>"TOTLSOLD.TXT"
Am I on the right track?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Massaging Textual Data

#18 Post by aGerman » 31 Oct 2015 14:54

Samir wrote:But is there a way to just nest the command instead of using a file?

No. What's the reason for that? Are you trying to skip a certain number of lines from the beginning?

Samir wrote:My thought is that

Code: Select all

>"!txt_out!"
could be changed to this to achieve it

Code: Select all

>>"TOTLSOLD.TXT"
Am I on the right track?

Yes you are.

Try

Code: Select all

@echo off &setlocal
set "txt_in=TOTLSOLD"
set "txt_out=TOTLSOLD.TXT"
set "skip=128"

setlocal EnableDelayedExpansion
<"!txt_in!" >>"!txt_out!" (
  for /l %%i in (1 1 %skip%) do set /p "="
  for %%i in (
    "Fuel Sales:87"
... etc.

Empty lines have to be counted for the "skip" variable too.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Massaging Textual Data

#19 Post by Samir » 31 Oct 2015 14:57

aGerman wrote:
Samir wrote:But is there a way to just nest the command instead of using a file?

No. What's the reason for that? Are you trying to skip a certain number of lines from the beginning?

Samir wrote:My thought is that

Code: Select all

>"!txt_out!"
could be changed to this to achieve it

Code: Select all

>>"TOTLSOLD.TXT"
Am I on the right track?

Yes you are.

Try

Code: Select all

@echo off &setlocal
set "txt_in=TOTLSOLD"
set "txt_out=TOTLSOLD.TXT"
set "skip=128"

setlocal EnableDelayedExpansion
<"!txt_in!" >>"!txt_out!" (
  for /l %%i in (1 1 %skip%) do set /p "="
  for %%i in (
    "Fuel Sales:87"
... etc.
Yes. The data that we need starts later in the file.

I'll try the changes and report back. 8)

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Massaging Textual Data

#20 Post by Samir » 31 Oct 2015 15:12

Everything works well. 8)

One other change that I wanted to do was to make sure that not only was an amount 0, but also the price 0.00 before removing the line. I'm thinking that changing this:

Code: Select all

      if "%%j" neq "0"  (
to this should do it?

Code: Select all

      if "%%j" neq "0" if "%%k" NEQ "0.00" (
I'm not sure if the .00 is relevant since it will probably be dropped if there's only an arithmetic comparison occurring. And if so, it would be best to do a string comparison, correct?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Massaging Textual Data

#21 Post by aGerman » 31 Oct 2015 15:28

Are you saying that an amount of 0 could cost more than 0 or an amount of more than 0 could be for free? I'm surprized.
Samir wrote:I'm not sure if the .00 is relevant since it will probably be dropped if there's only an arithmetic comparison occurring.

There is no numeric value in Batch that includes a point. For that reason a value with point will always be compared alphanumeric (nothing dropped).

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Massaging Textual Data

#22 Post by Samir » 31 Oct 2015 15:58

aGerman wrote:Are you saying that an amount of 0 could cost more than 0 or an amount of more than 0 could be for free? I'm surprized.
Samir wrote:I'm not sure if the .00 is relevant since it will probably be dropped if there's only an arithmetic comparison occurring.

There is no numeric value in Batch that includes a point. For that reason a value with point will always be compared alphanumeric (nothing dropped).
I'm saying that sometimes the system might mess up. :lol: Hence the double-check. So I'll implement my change and test it and report back. 8)

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Massaging Textual Data

#23 Post by aGerman » 31 Oct 2015 16:09

Samir wrote:I'm saying that sometimes the system might mess up.

Hmm. Your implementation is an AND connective. I assume you should better write an OR connective which can be simplified in your case

Code: Select all

if "%%j%%k" neq "00.00" (

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Massaging Textual Data

#24 Post by Samir » 31 Oct 2015 16:17

aGerman wrote:
Samir wrote:I'm saying that sometimes the system might mess up.

Hmm. Your implementation is an AND connective. I assume you should better write an OR connective which can be simplified in your case

Code: Select all

if "%%j%%k" neq "00.00" (
You're right! I forgot that an inverted AND is an OR. :oops: Good catch!

I like the catenated string compare too--never thought of doing it that way. 8)

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Massaging Textual Data

#25 Post by Samir » 02 Nov 2015 10:18

Everything works great, so now I've moved onto another segment of my task that requires other information to be extracted.

But I noticed the same data I need is actually created by this batch--specifically second column in these lines:

Code: Select all

Unl-Self            1752.757  4555.44
Ul-Plus-Self          84.941   234.36
Supreme-Self         232.519   711.28
Diesel-Self           19.281    53.00
which is also in the final file from the batch:

Code: Select all

Fuel Sales:87
1752.757
4555.44
Fuel Sales:89
84.941
234.36
Fuel Sales:93
232.519
711.28
Fuel Sales:Diesel
19.281
53.00[code]I need the data formatted as the following with a zero present if a column has zero in the source file:[code]
1752.757,84.941,232.519,19.281
I've thought of two approaches so far.

The first approach would be to modify the current batch to have an if statement that will check if the current line being processed is one of the ones I need and the copy the data into 4 variables that at the end of this batch I can echo to a file. There would have to be some modification to the zero checking in order to output zeros as necessary.

The second approach would be to mine the resulting file from the first batch, picking up the data from the appropriate line numbers.

Thoughts on the fastest/easiest approach?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Massaging Textual Data

#26 Post by aGerman » 02 Nov 2015 10:41

Code: Select all

@echo off &setlocal
set "txt_in=TOTLSOLD"
set "txt_out=TOTLSOLD.TXT"
set "txt_fuel=FUEL.TXT"
set "skip=128"

set "n=0"
set "fuel="
setlocal EnableDelayedExpansion
<"!txt_in!" >>"!txt_out!" (
  for /l %%i in (1 1 %skip%) do (set /p "=")
  for %%i in (
    "Fuel Sales:87"

... etc.

    "Non-Fuel Sales:Gift Card"
  ) do (
    set "ln=" &set /p "ln="
    if defined ln for /f "tokens=1,2" %%j in ("!ln:~12!") do (
      if !n! lss 4 (
        set /a "n+=1"
        set "fuel=!fuel!,%%j"
      )

      if "%%j%%k" neq "00.00" (
        echo %%~i
        echo %%j
        echo %%k
      )
    )
  )
)

>"!TXT_FUEL!" echo !fuel:~1!

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Massaging Textual Data

#27 Post by Samir » 02 Nov 2015 15:01

aGerman wrote:

Code: Select all

@echo off &setlocal
set "txt_in=TOTLSOLD"
set "txt_out=TOTLSOLD.TXT"
set "txt_fuel=FUEL.TXT"
set "skip=128"

set "n=0"
set "fuel="
setlocal EnableDelayedExpansion
<"!txt_in!" >>"!txt_out!" (
  for /l %%i in (1 1 %skip%) do (set /p "=")
  for %%i in (
    "Fuel Sales:87"

... etc.

    "Non-Fuel Sales:Gift Card"
  ) do (
    set "ln=" &set /p "ln="
    if defined ln for /f "tokens=1,2" %%j in ("!ln:~12!") do (
      if !n! lss 4 (
        set /a "n+=1"
        set "fuel=!fuel!,%%j"
      )

      if "%%j%%k" neq "00.00" (
        echo %%~i
        echo %%j
        echo %%k
      )
    )
  )
)

>"!TXT_FUEL!" echo !fuel:~1!
Ah yes! That's exactly what I was thinking except I forgot that I could do a whole if clause as opposed to one line and that variables can use themselves when updating. I'll test it and report back. 8)

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Massaging Textual Data

#28 Post by Samir » 02 Nov 2015 15:09

Worked great!

Post Reply