E=mc2
asked on
How can I modify this PS script in order to produce the desired output?
Source data:
PS Script to modify:
Output file desired:
TITLE OF DOCUMENT:
Number.: 0012-3456789-0123
ITEM1: 12345678945612, Qty: 13
ITEM1: 78945612378945, Qty: 14
Number.: 0012-3456789-0456
ITEM1: 78912345678945, Qty: 100
ITEM1: 78945612345678, Qty: 11
ISA*00* *00* *AA*COMPANYA *00*1234567891 *123456*1234*A*00123*000012345*0*A*>~
GS*AB*COMPANYA*1234564567*20150619*1234*12345*A*001234~
ST*100*123456789~
BSN*00*00000123*20150701*1234*0000~
HL*1**A~
TD5*B*2*VIA*A*VIA2~
REF*AB*12345~
REF*AB*123456789-1~
N1*ST**00*0123~
HL*2*1*O~
PRF*0012-3456789-0123~
PID*A**AB*AB~
TD1*ABC00*00~
N1*AB**01*0123~
HL*3*2*A~
MAN*GM*12345678912345678912~
HL*1*2*A~
PO4*12~
MAN*UC*12345678945612~
HL*5*4*I~
LIN**AB*123456789~
SN1**13*BX~
HL*3*4*A~
PO4*0~
MAN*UC*78945612378945~
HL*1*2*A~
LIN**AB*123456789~
SN1**14*BX~
CTT*0~
SE*01*123456789~
ST*100*23456789~
BSN*00*00000123*20150619*1234*0000~
HL*2**B~
TD5*A*1*VIA2*V*VIA3~
REF*AB*12345~
REF*AB*123456789-0~
N1*ST**01*0456~
HL*4*3*A~
PRF*0012-3456789-0456~
PID*A**AB*CD~
TD1*ABC10*01~
N1*AB**01*0456~
HL*1*2*A~
MAN*GM*45678912345678945612~
HL*1*2*A~
PO4*10~
MAN*UC*78912345678945~
HL*1*2*A~
LIN**AB*456789123~
SN1**100*BX~
HL*1*2*B~
PO4*00~
MAN*UC*78945612345678~
HL*9*8*C~
LIN**AB*123456789~
SN1**11*AB~
CTT*1~
SE*01*123456789~
GE*02*12345~
IEA*0*123456789~
~
PS Script to modify:
$InputFile = "C:\source\source.txt"
$OutputFile = "C:\output\output.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 "(BSN\*)|(SN1\*)|(PID\*)|(SE\*)|N1"}
}
Process{
$Data | % {
#Check the end of data set
If ($_ -match "N1\*"){
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$shipto$line`r`n$totline`r`n" | out-file $OutputFile -Encoding UTF8 -Append
#reset the variables
$line,$poline,$shipto,$totline,$total,$i,$n3,$n4 = $null,$null,$null,$null,$null,0,0,0
}
}
#Collect the PO Number
If($_ -match "PRF\*") {
$strArray = $_.split("*")
$currpo = $strArray[3]
$poline = "PO Number: $currpo"
}
#Collect details
If($_ -match "SN1\*{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`nItem $i`: UPC: $UPC, Desc: $Desc, Qty: $count Price `$$Price"
}
}
}
}
If(Test-Path $InputFile){
ParseText $OutputFile $InputFile
}
Output file desired:
TITLE OF DOCUMENT:
Number.: 0012-3456789-0123
ITEM1: 12345678945612, Qty: 13
ITEM1: 78945612378945, Qty: 14
Number.: 0012-3456789-0456
ITEM1: 78912345678945, Qty: 100
ITEM1: 78945612345678, Qty: 11
ASKER
@pony10us - Thanks however this was a different question.
Try this
#region
$InputFile = "C:\source\source.txt"
$OutputFile = "C:\output\output.txt"
#HashTable
$ProcessedPO = $null
$ReportedItems = $null
#endregion
Function ParseText ($OutputFile,$InputFile){
Begin{
$script:ProcessedPO = @{}
$script:ReportedItems = @{}
$line,$poline,$totline,$total,$i = $null,$null,$null,$null,0
Set-Content $OutputFile $null
"TITLE OF DOCUMENT:`r`n" | out-file $OutputFile -Encoding UTF8
$Data = Get-Content -Path $InputFile | ?{$_ -match "(BSN\*)|SN1|(PID\*)|(SE\*)|N1|PRF|MAN"}
}
Process{
$Data | % {
#Check the end of data set_
#Collect the PO Number
If($_ -match "PRF\*") {
$strArray = $_.split("*")
$currpo = $strArray[1]
$currpo = "PO Number: " + $currpo.Substring(0,$currpo.Length-1)
}
#Collect details
If($_ -match "MAN\*"){
If($_.Contains("UC")){
$strArray = $_.split("*")
$currItem = $strArray[2]
if(!($script:ProcessedPO.ContainsKey($currpo)))
{
$itemcnt = 1
$script:ProcessedPO.Add($currpo,1)
$currItem = "ITEM" + $itemcnt + ": " + $currItem.Substring(0,$currItem.Length-1)
}
else
{
$itemcnt++
$currItem = "ITEM" + $itemcnt + ": " + $currItem.Substring(0,$currItem.Length-1)
}
}
}
If($_ -match "SN1\*"){
$strArray = $_.split("*")
$total = $strArray[2]
$ProcessedItem = $currItem + ", Qty: " + $total
}
If ($_ -match "N1\*"){
If($currPO -ne $null -and $total -ne $null){
$CurrentItem = $CurrPO + "," + $currItem + ", Qty: " + $total
if(!($script:ReportedItems.ContainsKey($CurrentItem)))
{
$script:ReportedItems.Add($CurrentItem,1)
}
#reset the variables
$line,$ProcessedItem,$poline,$shipto,$totline,$total,$i,$n3,$n4 = $null,$null,$null,$null,$null,$null,0,0,0
}
}
}
}
}
If(Test-Path $InputFile){
ParseText $OutputFile $InputFile
}
foreach ($item in $ProcessedPO.Keys) {
"$item`r" | out-file $OutputFile -Encoding UTF8 -Append
foreach ($procitem in $ReportedItems.Keys) {
If($procitem -match $item){
($po,$rest) = $procitem.split(',',2)
"$rest`r" | out-file $OutputFile -Encoding UTF8 -Append
}
}
"`r" | out-file $OutputFile -Encoding UTF8 -Append
}
ASKER
Thanks, this works, however the order of the items is not consistent, sometimes it's listed as ITEM2, 3 and 1 and sometimes 2, 1..
I'll update to have it sorted
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Looks much better.
Is there any way it could sort it in the order of the PO number?
If not, that is fine, it looks good.
Thanks,
Is there any way it could sort it in the order of the PO number?
If not, that is fine, it looks good.
Thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent, thank you.
https://www.experts-exchange.com/questions/28691831/How-can-I-add-a-page-heading-in-the-following-sample-Powershell-script.html?anchorAnswerId=40847086#a40847086