Solved

How can I add various strings that appear in a text file?

Posted on 2014-09-09
10
185 Views
Last Modified: 2014-09-11
I would like a script to look at a text file, line by line, and where it finds the following strings, it should add the amount which immediately follows.

A sample of what part of the lines might look like are the following.

..
..
LO1**540*CA*45.0500** etc..
..
LO1**200*CA*27.1200**etc..
..

Whenever the script sees a line which starts with LO1, it should then take the number that follows the two ** and then multiply it by the next number which appears in the line.  In the above case, the first line would be 540*45.05.. and so forth for every line.

The resulting data should be the total of all the multiplied lines in a new text file, which perhaps shows the first line of data which was found in the file, and then maybe on the second line, it should show the total.

For instance, the contents of the new text could be..

contents of the first line in the file
Total Dollar Value:   $29751.00
0
Comment
Question by:100questions
  • 5
  • 5
10 Comments
 

Author Comment

by:100questions
ID: 40313033
Currently, I am able to look through the file and find the lines which have the data, however I am not able to do any calculations with the data.

I use something similar to this batch file.

pushd "C:\Data"
if exist *.txt findstr /c:"LO1**" *.txt >>NewData.txt || del NewData.txt
popd

The output is something that looks like this..

Original.txt:LO1**200*CA*20.01**ABC*~
Original.txt:LO1**1*CA*7.52**ABC*~
Original.txt:LO1**10*CA*1.50**ABC*~
Original.txt:LO1**2*CA*1.50**ABC*~

This is not enough however, I need more, as I need to be able to use the data and get totals, as outline in my initial email above.

Any assistance is appreciated.
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 40313752
Calculations in batch can be cone, but are somewhat limited, especially when it's not with integers.
Would you mind Powershell in this case?
$InputFile = "C:\Data\original.txt"
$OutputFile = "C:\Data\NewData.txt"
[double]$Total = 0
($Input = Get-Content -Path $InputFile) | ? {$_ -match "^LO1"} | % {
	$Columns = $_.Split("*")
	$Total += ([double]$Columns[2]) * [double]($Columns[4])
}
$Input[0], ("Total Dollar Value: {0:C}" -f $Total) | Set-Content -Path $OutputFile -Encoding UTF8
Get-Content -Path $OutputFile

Open in new window

0
 

Author Comment

by:100questions
ID: 40313856
PowerShell may not be the best option, however I can try.  It's s Windows 8 os.
0
 
LVL 83

Expert Comment

by:oBdA
ID: 40313906
In case you haven't worked with PS yet, basically the first thing you need to do is open a PS console and enter (you have tab completion for commands and most arguments)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Open in new window

You can set that for the machine if the console is started elevated; just leave out the "-Scope CurrentUser", default is "LocalMachine".
More information about this is available using
Get-Help about_Execution_Policies -ShowWindow

Open in new window

Note that unlike batch, if you're in the console, you always need to add the path to the script; so even if you're in the script's folder, use ".\SomeScript.ps1". This is for security reasons and due to the fact that in PS, the current "drive" can come from providers other than the file system (for example HKLM:). Details in
Get-Help about_Command_Precedence -ShowWindow

Open in new window

Granted Powershell takes some getting used to, but Microsoft did a really good job with it.
0
 

Author Comment

by:100questions
ID: 40313953
Should I try this first and then I can ask for a Powershell script?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 83

Expert Comment

by:oBdA
ID: 40313965
Should I try this first and then I can ask for a Powershell script?
Sorry, you've lost me.
The script in http:#a40313752 is a Powershell script. Save it as Whatever.ps1 (adjusting the file names, if required), for example in C:\Data.
Then open a Powershell console, and enter (copy and paste will work)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force

Open in new window

Then enter (you can use the good old "cd" as well, which is an alias for Set-Location)
Set-Location C:\Data

Open in new window

Finally, to start the script, just enter
.\Whatever.ps1

Open in new window

0
 

Author Comment

by:100questions
ID: 40314037
Thanks.  Can I call the script from within a regular .bat file?
0
 
LVL 83

Expert Comment

by:oBdA
ID: 40314049
Sure; this assumes that the batch script and the ps script are in the same folder (this sets the execution policy only for the execution of the script):
set PSFile=%~dp0Whatever.ps1
PowerShell.exe -ExecutionPolicy Bypass -Command "& '%PSFile%'"

Open in new window

0
 

Author Comment

by:100questions
ID: 40314053
And when it performs the function of the powershell script will it continue with the rest of the bat file?
0
 
LVL 83

Expert Comment

by:oBdA
ID: 40314055
Yep; just like (most) other calls to external programs.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Connect TV screen in another room 6 65
Windows doesn't download updates 26 91
Communication with an active RFID 7 63
How do I promote a cleaning service best? 16 61
Stuck in voice control mode on your Amazon Firestick?  Here is how to turn it off!!!
Whether you believe the “gig economy,” as it has been dubbed, is the next big economic paradigm shift (https://www.theguardian.com/commentisfree/2015/jul/26/will-we-get-by-gig-economy) or an overstated trend (http://www.wsj.com/articles/proof-of-a-g…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now