Solved

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

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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
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.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
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, f…

920 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

15 Experts available now in Live!

Get 1:1 Help Now