E=mc2
asked on
How can I modify this script to product a summary based on items
I would like to modify the script below so that the output is similar to the following.
Desired type of Output:
UPC: 123456123123, Total Qty: 200, Price per UPC $1.00, Total $ Value: $ 200.00
UPC: 123456123546, Total Qty: 300, Price per UPC $3.00, Total $ Value: $ 900.00
UPC: 122313213213, Total Qty: 100, Price per UPC $0.50, Total $ Value: $ 50.00
Total PO Value: $1150.00
$InputFile = "C:\folder\original.txt"
$OutputFile = "C:\folder\modified.txt"
Function ParseText ($OutputFile,$InputFile){
Begin{
$line,$totline,$total,$i = $null,$null,$null,$null,0
Set-Content $OutputFile $null
$Data = Get-Content -Path $InputFile | ?{$_ -match "(BEG\*)|(PO1\*)|(PID\*)|(SE\*)|(IEA\*)|(ISA\*)|N1"}
}
Process{
$Data | % {
#Check the end of data set
If ($_ -match "N1"){
$strArray = $_.split("*")
$currString = $strArray[2] -replace '\s+', ' '
$splitString = $currString.split(" ")
$stringCnt = $splitString.count - 1
$i = 0
for ($i = 0; $i -le $stringCnt - 1; $i++)
{
$companyName += $splitString[$i] + " "
}
}
If ($_ -match "IEA\*"){
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 "ISA\*" -and $_ -match "\*(\w{2}\**)(\d{3,10})"){
$poline = ""
}
#Collect details
If($_ -match "PO1\*{1,}\d\*(\d{1,})\*\w{1,}\*(\d{1,}(\.\d{1,})?).*\*(\d{1,})~"){
$count = $Matches[1]
$Price = $Matches[2]
$UPC = $Matches[4]
$total += [double]$count * [double]$Matches[2]
}
If($_ -match "PID\*"){
$i++
$Desc = $_ -Replace "PID\*\w\*{1,}(\d+\*{1,})?|<NL>|~"
$line += "`r`nUPC: $UPC, Qty: $count Price `$$Price"
}
}
}
}
If(Test-Path $InputFile){
ParseText $OutputFile $InputFile
}
Desired type of Output:
UPC: 123456123123, Total Qty: 200, Price per UPC $1.00, Total $ Value: $ 200.00
UPC: 123456123546, Total Qty: 300, Price per UPC $3.00, Total $ Value: $ 900.00
UPC: 122313213213, Total Qty: 100, Price per UPC $0.50, Total $ Value: $ 50.00
Total PO Value: $1150.00
Attach the sample input file and the current output file
ASKER
Here is a sample input file:
ISA*00*0000000000*00*00000 00000*01*1 2345612378 *AA*ABCDEFGH *123456*1234*P*00123*00000 0012*1*Q*> ~
GS*PO*8888888888*ABCDEFGH* 20150101*0 123*9*X*00 1234~
ST*850*123456789~
BEG*00*SA*1234-2222222-123 4**2015010 1*Q~
REF*IA*1234567*COMPANY~
REF*DP*012~
FOB*DF*OR*CITY~
CSH*Y~
ITD*01*9*****99~
DTM*037*20150101~
DTM*038*20150101~
TD5*B*92*SOMETHING~
N1*BY*COMPANY*11*0123~
N3*ADDRESS1~
N4*TOWN*CITY*12345*SOMEWHE RE~
PO1*1*2*CA*1.01*UM*AB*1234 56456*AB*d esc*UP*123 456123456~
CTP*AB*ABC*1.00********9~
PID*F*01***desc2~
PO4*12~
PO1*2*5*CA*1.00*UM*AB*1234 56444*AB*d esc*UP*999 999111111~
CTP*AB*ABC*0.50********9~
PID*F*02***desc2b~
PO4*5~
CTT*1*99~
SE*11*123154641~
ST*850*88888888~
BEG*00*SA*1234-1111111-234 5**2015010 1*Q~
REF*IA*1234567*COMPANY~
REF*DP*013~
FOB*DF*OR*CITY~
CSH*Y~
ITD*01*0*****00~
DTM*037*20150101~
DTM*038*20150101~
TD5*X*11*ABCD*M~
N1*BY*COMPANY*11*0123~
N3*ADDRESS1~
N4*TOWN*CITY*12345*SOMEWHE RE~
PO1*1*1*CA*1.70*UM*AB*1234 56654*AB*d escabc*UP* 1234561234 56~
CTP*RS*ABC*0.75********1~
PID*F*08***desc~
PO4*12~
PO1*2*5*CA*5.04*UM*AB*4567 89456*AB*d escddd*UP* 9999991111 11~
CTP*AB*ABC*1.01********1~
PID*F*08***desc~
PO4*12~
PO1*3*2*CA*0.50*UM*AB*4561 23456*AB*d escbcd*UP* 9999945654 44~
CTP*AB*ABC*1.11********1~
PID*F*11***desc~
PO4*5~
CTT*1*19~
SE*01*123456789~
GE*01*1~
IEA*1*123456789~
~
ISA*00*0000000000*00*00000
GS*PO*8888888888*ABCDEFGH*
ST*850*123456789~
BEG*00*SA*1234-2222222-123
REF*IA*1234567*COMPANY~
REF*DP*012~
FOB*DF*OR*CITY~
CSH*Y~
ITD*01*9*****99~
DTM*037*20150101~
DTM*038*20150101~
TD5*B*92*SOMETHING~
N1*BY*COMPANY*11*0123~
N3*ADDRESS1~
N4*TOWN*CITY*12345*SOMEWHE
PO1*1*2*CA*1.01*UM*AB*1234
CTP*AB*ABC*1.00********9~
PID*F*01***desc2~
PO4*12~
PO1*2*5*CA*1.00*UM*AB*1234
CTP*AB*ABC*0.50********9~
PID*F*02***desc2b~
PO4*5~
CTT*1*99~
SE*11*123154641~
ST*850*88888888~
BEG*00*SA*1234-1111111-234
REF*IA*1234567*COMPANY~
REF*DP*013~
FOB*DF*OR*CITY~
CSH*Y~
ITD*01*0*****00~
DTM*037*20150101~
DTM*038*20150101~
TD5*X*11*ABCD*M~
N1*BY*COMPANY*11*0123~
N3*ADDRESS1~
N4*TOWN*CITY*12345*SOMEWHE
PO1*1*1*CA*1.70*UM*AB*1234
CTP*RS*ABC*0.75********1~
PID*F*08***desc~
PO4*12~
PO1*2*5*CA*5.04*UM*AB*4567
CTP*AB*ABC*1.01********1~
PID*F*08***desc~
PO4*12~
PO1*3*2*CA*0.50*UM*AB*4561
CTP*AB*ABC*1.11********1~
PID*F*11***desc~
PO4*5~
CTT*1*19~
SE*01*123456789~
GE*01*1~
IEA*1*123456789~
~
ASKER
Here is a sample output file, which needs to be changed:
UPC: 123456123456, Qty: 2 Price $1.01
UPC: 999999111111, Qty: 5 Price $1.00
UPC: 123456123456, Qty: 1 Price $1.70
UPC: 999999111111, Qty: 5 Price $5.04
UPC: 999994565444, Qty: 2 Price $0.50
Total: $34.92
UPC: 123456123456, Qty: 2 Price $1.01
UPC: 999999111111, Qty: 5 Price $1.00
UPC: 123456123456, Qty: 1 Price $1.70
UPC: 999999111111, Qty: 5 Price $5.04
UPC: 999994565444, Qty: 2 Price $0.50
Total: $34.92
$InputFile = "C:\folder\original.txt"
$OutputFile = "C:\folder\modified.txt"
Function ParseText ($OutputFile,$InputFile){
Begin{
$line,$totline,$total,$i = $null,$null,$null,$null,0
Set-Content $OutputFile $null
$Data = Get-Content -Path $InputFile | ?{$_ -match "(BEG\*)|(PO1\*)|(PID\*)|(SE\*)|(IEA\*)|(ISA\*)|N1"}
}
Process{
$Data | % {
#Check the end of data set
If ($_ -match "N1"){
$strArray = $_.split("*")
$currString = $strArray[2] -replace '\s+', ' '
$splitString = $currString.split(" ")
$stringCnt = $splitString.count - 1
$i = 0
for ($i = 0; $i -le $stringCnt - 1; $i++)
{
$companyName += $splitString[$i] + " "
}
}
If ($_ -match "IEA\*"){
If($poline -ne $null -and $total -ne $null){
$Totline = "Total PO Value: `$$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 "ISA\*" -and $_ -match "\*(\w{2}\**)(\d{3,10})"){
$poline = ""
}
#Collect details
If($_ -match "PO1\*{1,}\d\*(\d{1,})\*\w{1,}\*(\d{1,}(\.\d{1,})?).*\*(\d{1,})~"){
$count = $Matches[1]
$Price = $Matches[2]
$UPC = $Matches[4]
$total += [double]$count * [double]$Matches[2]
}
If($_ -match "PID\*"){
$i++
$Desc = $_ -Replace "PID\*\w\*{1,}(\d+\*{1,})?|<NL>|~"
$Subtotal = [double] $price * [int] $count
$strSubtotal = "{0:C2}" -f $Subtotal
$line += "`r`nUPC: $UPC,"
$line +=' Total Qty: '+ $count
$line +=' Price per UPC $' + $Price
$line +=' Total $ Value: ' + $strSubtotal
}
}
}
}
If(Test-Path $InputFile){
ParseText $OutputFile $InputFile
}
UPC: 123456123456, Total Qty: 2 Price per UPC $1.01 Total $ Value: $2.02
UPC: 999999111111, Total Qty: 5 Price per UPC $1.00 Total $ Value: $5.00
UPC: 123456123456, Total Qty: 1 Price per UPC $1.70 Total $ Value: $1.70
UPC: 999999111111, Total Qty: 5 Price per UPC $5.04 Total $ Value: $25.20
UPC: 999994565444, Total Qty: 2 Price per UPC $0.50 Total $ Value: $1.00
Total PO Value: $34.92
ASKER
Thanks David,
What I am hoping to achieve is an amalgamation of UPC's, such as this:
NB - My mistake. If multiple UPCs are listed, and they are the same, they should have the same price, therefore the output you have should really not occur, unless the each price is different.
So for instance the UPC ending in 456, should all be $1.01, and therefore should only show as a summary. The UPC ending in 111 should be $1.00. - - IF the price is different, even though it is the same UPC then yes, it should output as a separate line, otherwise.
Therefore, the output should really look like this:
UPC: 123456123456, Total Qty: 3 Price per UPC $1.01 Total $ Value: $3.03
UPC: 999999111111, Total Qty: 10 Price per UPC $1.00 Total $ Value: $10.00
UPC: 999994565444, Total Qty: 2 Price per UPC $0.50 Total $ Value: $1.00
Total PO Value: $14.03
Can the script be modified to show summaries instead, as above?
Thank you.
What I am hoping to achieve is an amalgamation of UPC's, such as this:
NB - My mistake. If multiple UPCs are listed, and they are the same, they should have the same price, therefore the output you have should really not occur, unless the each price is different.
So for instance the UPC ending in 456, should all be $1.01, and therefore should only show as a summary. The UPC ending in 111 should be $1.00. - - IF the price is different, even though it is the same UPC then yes, it should output as a separate line, otherwise.
Therefore, the output should really look like this:
UPC: 123456123456, Total Qty: 3 Price per UPC $1.01 Total $ Value: $3.03
UPC: 999999111111, Total Qty: 10 Price per UPC $1.00 Total $ Value: $10.00
UPC: 999994565444, Total Qty: 2 Price per UPC $0.50 Total $ Value: $1.00
Total PO Value: $14.03
Can the script be modified to show summaries instead, as above?
Thank you.
are you familiar with the term GIGO (garbage in garbage out)
How is the program to determine the price of an item if it changes throughout the document? Where is the data validated? Lowest price?
Are we both looking at the same original.txt ?
PO1*1*2*CA*1.01*UM*AB*1234 56456*AB*d esc*UP*123 456123456~
PO1*2*5*CA*1.00*UM*AB*1234 56444*AB*d esc*UP*999 999111111~
PO1*1*1*CA*1.70*UM*AB*1234 56654*AB*d escabc*UP* 1234561234 56~
*456 has 2 prices 1.01 and 1.70
*111 again 2 prices 1.00 and 5.04
Are they the same SKU or is one per item and the other per case?
PO1*2*5*CA*5.04*UM*AB*4567 89456*AB*d escddd*UP* 9999991111 11~
PO1*3*2*CA*0.50*UM*AB*4561 23456*AB*d escbcd*UP* 9999945654 44~
PO1*1*
2 [QTY]
*CA* [Dollar?]
1.01 [Price]
*UM*AB*123456456*AB*desc*U P*
123456123456 [UPC]
~
How is the program to determine the price of an item if it changes throughout the document? Where is the data validated? Lowest price?
Are we both looking at the same original.txt ?
PO1*1*2*CA*1.01*UM*AB*1234
PO1*2*5*CA*1.00*UM*AB*1234
PO1*1*1*CA*1.70*UM*AB*1234
*456 has 2 prices 1.01 and 1.70
*111 again 2 prices 1.00 and 5.04
Are they the same SKU or is one per item and the other per case?
PO1*2*5*CA*5.04*UM*AB*4567
PO1*3*2*CA*0.50*UM*AB*4561
PO1*1*
2 [QTY]
*CA* [Dollar?]
1.01 [Price]
*UM*AB*123456456*AB*desc*U
123456123456 [UPC]
~
ASKER
A mistake was made on the input text showing the prices as different for the same UPCs.
The prices will always be the same for identical UPCs.
A summary of each SKU then, which should only show up once is what is the output file should show.
Thank you.
The prices will always be the same for identical UPCs.
A summary of each SKU then, which should only show up once is what is the output file should show.
Thank you.
upload a new input file
ASKER
New input file:
ISA*00*0000000000*00*00000 00000*01*1 2345612378 *AA*ABCDEFGH *123456*1234*P*00123*00000 0012*1*Q*> ~
GS*PO*8888888888*ABCDEFGH* 20150101*0 123*9*X*00 1234~
ST*850*123456789~
BEG*00*SA*1234-2222222-123 4**2015010 1*Q~
REF*IA*1234567*COMPANY~
REF*DP*012~
FOB*DF*OR*CITY~
CSH*Y~
ITD*01*9*****99~
DTM*037*20150101~
DTM*038*20150101~
TD5*B*92*SOMETHING~
N1*BY*COMPANY*11*0123~
N3*ADDRESS1~
N4*TOWN*CITY*12345*SOMEWHE RE~
PO1*1*2*CA*0.75*UM*AB*1234 56456*AB*d esc*UP*123 456123456~
CTP*AB*ABC*0.10********9~
PID*F*01***desc2~
PO4*12~
PO1*2*5*CA*1.00*UM*AB*1234 56444*AB*d esc*UP*999 999111111~
CTP*AB*ABC*0.50********9~
PID*F*02***desc2b~
PO4*5~
CTT*1*99~
SE*11*123154641~
ST*850*88888888~
BEG*00*SA*1234-1111111-234 5**2015010 1*Q~
REF*IA*1234567*COMPANY~
REF*DP*013~
FOB*DF*OR*CITY~
CSH*Y~
ITD*01*0*****00~
DTM*037*20150101~
DTM*038*20150101~
TD5*X*11*ABCD*M~
N1*BY*COMPANY*11*0123~
N3*ADDRESS1~
N4*TOWN*CITY*12345*SOMEWHE RE~
PO1*1*1*CA*0.75*UM*AB*1234 56654*AB*d escabc*UP* 1234561234 56~
CTP*XY*ABC*0.10********1~
PID*F*08***desc~
PO4*12~
PO1*2*5*CA*1.00*UM*AB*4567 89456*AB*d escddd*UP* 9999991111 11~
CTP*AB*ABC*0.50********1~
PID*F*08***desc~
PO4*10~
PO1*3*2*CA*0.50*UM*AB*4561 23456*AB*d escbcd*UP* 9999945654 44~
CTP*AB*ABC*1.11********1~
PID*F*11***desc~
PO4*5~
CTT*1*99~
SE*01*123456789~
GE*01*1~
IEA*1*123456789~
~
ISA*00*0000000000*00*00000
GS*PO*8888888888*ABCDEFGH*
ST*850*123456789~
BEG*00*SA*1234-2222222-123
REF*IA*1234567*COMPANY~
REF*DP*012~
FOB*DF*OR*CITY~
CSH*Y~
ITD*01*9*****99~
DTM*037*20150101~
DTM*038*20150101~
TD5*B*92*SOMETHING~
N1*BY*COMPANY*11*0123~
N3*ADDRESS1~
N4*TOWN*CITY*12345*SOMEWHE
PO1*1*2*CA*0.75*UM*AB*1234
CTP*AB*ABC*0.10********9~
PID*F*01***desc2~
PO4*12~
PO1*2*5*CA*1.00*UM*AB*1234
CTP*AB*ABC*0.50********9~
PID*F*02***desc2b~
PO4*5~
CTT*1*99~
SE*11*123154641~
ST*850*88888888~
BEG*00*SA*1234-1111111-234
REF*IA*1234567*COMPANY~
REF*DP*013~
FOB*DF*OR*CITY~
CSH*Y~
ITD*01*0*****00~
DTM*037*20150101~
DTM*038*20150101~
TD5*X*11*ABCD*M~
N1*BY*COMPANY*11*0123~
N3*ADDRESS1~
N4*TOWN*CITY*12345*SOMEWHE
PO1*1*1*CA*0.75*UM*AB*1234
CTP*XY*ABC*0.10********1~
PID*F*08***desc~
PO4*12~
PO1*2*5*CA*1.00*UM*AB*4567
CTP*AB*ABC*0.50********1~
PID*F*08***desc~
PO4*10~
PO1*3*2*CA*0.50*UM*AB*4561
CTP*AB*ABC*1.11********1~
PID*F*11***desc~
PO4*5~
CTT*1*99~
SE*01*123456789~
GE*01*1~
IEA*1*123456789~
~
ASKER
New desired output file:
UPC: 123456123456, Total Qty: 3 Price per UPC $0.75 Total $ Value: $2.25
UPC: 999999111111, Total Qty: 10 Price per UPC $1.00 Total $ Value: $10.00
UPC: 999994565444, Total Qty: 2 Price per UPC $0.50 Total $ Value: $1.00
Total PO Value: $13.25
UPC: 123456123456, Total Qty: 3 Price per UPC $0.75 Total $ Value: $2.25
UPC: 999999111111, Total Qty: 10 Price per UPC $1.00 Total $ Value: $10.00
UPC: 999994565444, Total Qty: 2 Price per UPC $0.50 Total $ Value: $1.00
Total PO Value: $13.25
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.