Solved

How can I get the PowerShell script to calculate data correcty?

Posted on 2014-09-16
2
166 Views
Last Modified: 2014-09-17
Some source data:

AIS*00*          *00*          *01*1234567890     *99*1234567890     *12345*1234*P*00123*000000123*0*Q*>~
GS*OG*1234567890*1234567890*20140916*1234*123*Y*123456PQR~
ST*999*0123~
G50*N*20140916*444444~
G61*PQ*INFO*PQ*12345678912~
G62*02*20141212~
NTE*NOT*NOTE~
G66**T*****NONE~
G23*01*2**01*0**0**30~
N1*BT*NAME*9*1234567891234~
N2*LOC                0021345~
N3*ADDRESS~
N4*INFO*QR*12345~
N1*ST*NAME*9*1234567891234~
N2*LOC            123~
N3*STREET~
N4*LOC*AB*12345~
N1*VN*NAME/ABA*8*1234567891234~
N2*STREET~
N4*LOC**12345~
G48*10*CA*10.05*123456789123*IN*123456~
G49*INFO~
G40*5*2*KG******123456~
G48*25*CA*15.00*12345678956*IN*654321~
G49*INFO~
G76*35*CA*1.1*KP*100.64*TR**1234567~
SE*26*1234~
GE*1*326~
IEP*1*000000123~


Sample Powershell script:

$InputFile = "C:\data\original.txt"
$OutputFile = "C:\data\newdata.txt"

Function ParseText ($OutputFile,$InputFile){
Begin{
$newdataline,$totline,$total = $NULL,$NULL,$NULL
Set-Content $OutputFile $null
}
Process{
      Get-Content -Path $InputFile | % {

If ($_ -match "AIS\*1"){
      If ($poline -ne $null -and $total -ne $null){
      $Totline = "Total: `$$Total"
      write-host "This is Total `$$total"
            "`n$newdataline `r`n$totline" | out-file $OutputFile -Encoding UTF8 -Append
            $newdataline,$totline,$total = $NULL,$NULL,$NULL
      }
}
   If($_ -match "(G50\*)(\d{0,10})") {
      $newdataline = "New Data: $($Matches[2])"
    }Elseif ($_ -match "G48"){
        $col=$_.split("*") ; $total += [double]$col[2] * [double]$col[4]
  }
 }
}
}

if (Test-Path $InputFile){
     ParseText $OutputFile $InputFile
}


Desired output:

New Data:  44444 (from the G50 line)
Total:  $ (the total of the data in the G48 lines.  (ie as per above - 10 x 10.05 + 25 * 15.00)

What modification needs to be made to the script so that the newdata.txt file is showing accurate data?
0
Comment
Question by:100questions
2 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 40326519
based on the input data, following code should give you the expected result..
$InputFile = "C:\data\original.txt"
$OutputFile = "C:\data\newdata.txt"

Function ParseText ($OutputFile,$InputFile){
Begin{
 $newdataline,$totline,$total = $NULL,$NULL,$NULL
 Set-Content $OutputFile $null
}
Process{
Get-Content -Path $InputFile | % {
  If ($_ -match "IEP\*1"){
    If ($newdataline -ne $null -and $total -ne $null){
      $Totline = "Total: `$$Total"
      write-host "This is Total `$$total"
      "`n$newdataline `r`n$totline" | out-file $OutputFile -Encoding UTF8 -Append
      $newdataline,$totline,$total = $NULL,$NULL,$NULL
      }
 }
   If($_ -match "(G50\*N\*\d{1,}\*)(\d{0,10})") {
    $newdataline = "New Data: $($Matches[2])"
    }Elseif ($_ -match "G48"){
    $col=$_.split("*") ; $total += [double]$col[1] * [double]$col[3]
  }
 }
}
}

if (Test-Path $InputFile){
     ParseText $OutputFile $InputFile
}

Open in new window

Result..
New Data: 444444 
Total: $475.5

Open in new window

0
 

Author Closing Comment

by:100questions
ID: 40328766
Excellent! Thank you.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This article will help you understand what HashTables are and how to use them in PowerShell.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

705 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

18 Experts available now in Live!

Get 1:1 Help Now