Solved

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

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This article will help you understand what HashTables are and how to use them in PowerShell.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

832 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