Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-09-16
2
Medium Priority
?
176 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 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

824 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