Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Further modification to the current Powershell script

Posted on 2014-09-19
8
Medium Priority
?
121 Views
Last Modified: 2014-09-19
Modification to a script needs to be made to adjust to these changes in the source data.

Where the PO number is:
G50*N*20140916*999999~     (the position of the PO number is represented by 999999)

Where the PO1 and the PID used to be, now this is showing:

G80*10*CA*15.22*123456789123*IN*123456~
G81*DESC~


The script to work with:

$InputFile = "C:\original.txt"
$OutputFile = "C:\New.txt"

Function ParseText ($OutputFile,$InputFile){
Begin{
            $line,$poline,$totline,$total,$i = $null,$null,$null,$null,0
            Set-Content $OutputFile $null
            $Data = Get-Content -Path $InputFile | ?{$_ -match "(G50\*)|(G81\*)|(G80\*{2})|(EFG\*)"}
      }
Process{
      $Data | % {
      #Check the end of data set
            If ($_ -match "EFG\*"){
                  If($poline -ne $null -and $total -ne $null){
                        $Totline = "Total: `$$Total"
                        Write-host "This is Total `$$total"
                        #Write the data collection to output file
                        "$poline$line`r`n$totline`r`n" | out-file $OutputFile -Encoding UTF8 -Append
                        #reset the variables
                        $line,$poline,$totline,$total,$i = $null,$null,$null,$null,0
                  }
            }
            #Collect the PO Number
            If($_ -match "(G50\*)(\d{0,10})"){
                  $poline = "PONumber: $($Matches[2])"
            }
            #Collect  G80 details and following G81 Description
            If($_ -match "(G80\*{2})(\d{1,})\*(\w{1,})\*(\d{1,}(\.\d{1,})?)\*{2}(\w{1,})\*(\d{1,})"){
                  $i++
                  $total += [double]$Matches[2] * [double]$Matches[4]
                  $Desc = ($Data[([Array]::IndexOf($Data,$_)+1)] -split "\*\*\*\*|~")[1]
                  [String[]]$line += "`r`nItem $i`: UPC: $($Matches[7]), Desc: $Desc, Qty: $($Matches[2]) Price  `$$($Matches[4])"
            }
      }
 }
}

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


Desired type of output:

PONumber: 999999
   Item 1: UPC: 12345678912345, Desc: information here, Qty: 248 Price: $10.00
   Item 2: UPC: 23456789123452, Desc: information here, Qty: 329 Price: $1.00
   Item 3: UPC: 98765432198765, Desc: information here, Qty: 20 Price $2.00
   Total: $Calculated total

  PONumber: 999985
   Item 1: UPC: 12345678912345, Desc: information here, Qty: 250 Price: $10.00
   Item 2: UPC: 23456789123452, Desc: information here, Qty: 200 Price: $1.00
   Item 3: UPC: 98765432198765, Desc: information here, Qty: 100 Price $2.00
   Total: $Calculated total


What changes need to be made to this script in order to produce the desired output?
0
Comment
Question by:100questions
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 40332603
What is the UPC code from line

G80*10*CA*15.22*123456789123*IN*123456~
0
 

Author Comment

by:100questions
ID: 40332620
In this example it's:  123456789123
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 2000 total points
ID: 40332653
Try..
$InputFile = "C:\original.txt"
$OutputFile = "C:\New.txt"

 Function ParseText ($OutputFile,$InputFile){
 Begin{
             $line,$poline,$totline,$total,$i = $null,$null,$null,$null,0
             Set-Content $OutputFile $null
             $Data = Get-Content -Path $InputFile | ?{$_ -match "(G50\*)|(G81\*)|(G80\*)|(EFG\*)"}
       }
 Process{
       $Data | % {
       #Check the end of data set
             If ($_ -match "EFG\*"){
                   If($poline -ne $null -and $total -ne $null){
                         $Totline = "Total: `$$Total"
                         Write-host "This is Total `$$total"
                         #Write the data collection to output file
                         "$poline$line`r`n$totline`r`n" | out-file $OutputFile -Encoding UTF8 -Append
                         #reset the variables
                         $line,$poline,$totline,$total,$i = $null,$null,$null,$null,0
                   }
             }
             #Collect the PO Number
             If($_ -match "(\w{2}\*)(\d{3,10})"){
                   $poline = "PONumber: $($Matches[2])"
             }
             #Collect  G80 details and following G81 Description
             If($_ -match "(G80\*{1,})(\d{1,})\*(\w{1,})\*(\d{1,}(\.\d{1,})?)\*{1,}(\w{1,}|\d{1,})"){
                   $i++
                   $total += [double]$Matches[2] * [double]$Matches[4]
                   $Desc = ($Data[([Array]::IndexOf($Data,$_)+1)] -split "\*{1,}|~")[1]
                   [String[]]$line += "`r`nItem $i`: UPC: $($Matches[6]), Desc: $Desc, Qty: $($Matches[2]) Price  `$$($Matches[4])"
             }
       }
  }
 }

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

Open in new window

0
Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

 

Author Comment

by:100questions
ID: 40332871
This almost works, except for where the PO number should appear, it's showing the quantity for the last item in my data, essentially duplicating the qty showing for item 2, and also showing it where the PO number is.

Sample output:

PONumber: 219
Item 1: UPC: 123456789123, Desc: DESC, Qty: 10 Price  $15.21
Item 2: UPC: 123456789500, Desc: DESC 2, Qty: 219 Price  $1.00
Total: $371.10
0
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 total points
ID: 40332905
Change line 24 to..
If($_ -match "G50\*" -and $_ -match "(\w{2}\*)(\d{3,10})"){

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 40332990
BTB, if you have too many such files to phrase, then I would suggest you open a question with all possible combinations of  'PO number, lines, PO1 lines, PID lines and the end of dataset. Then we can try to come up with a code which can deal with all these log files.
0
 

Author Comment

by:100questions
ID: 40333011
Thanks .. I don't think I have to many more at this time, however I will keep this in mind for sure.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40333022
Ok.. No problem.. :-)
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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 antispam), the admini…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 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