Solved

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

Posted on 2014-09-16
2
169 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

860 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