Solved

How can I modify this Powershell script to show the data in the N1 segment?

Posted on 2014-10-23
3
95 Views
Last Modified: 2014-10-27
Script I am working with:

$InputFile = "C:\Data\Original.txt"
$OutputFile = "C:\Data\New.txt"

 Function ParseText ($OutputFile,$InputFile){
 Begin{
             $line,$poline,$shipto,$totline,$total,$i,$n3,$n4 = $null,$null,$null,$null,$null,0,0,0
             Set-Content $OutputFile $null
             $Data = Get-Content -Path $InputFile | ?{$_ -match "(G50\*)|(G69\*)|(G68\*)|(N\d\*)|(IEA\*)"}
       }
 Process{
       $Data | % {
       #Check the end of data set
             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$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 "G50\*" -and $_ -match "(\w{2}\*)(\d{3,10})"){
                   $poline = "PONumber: $($Matches[2])"
             }  
             #Collect  G68 details and following G69 Description
             If($_ -match "(G68\*{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


I have some N1 segments in the source script which look similar to this:

N1*ST*LOCATION 1*2*1234567891234~
N1*TT*COMPANY NAME*3*1234567891234~
N1*VV*SHIPT TO CO NAME*11*AB123456~

I would like the output to look at the first N1 segment and then the output should be similar to this:

PONumber: 123456
Ship to:  LOCATION
Item 1: UPC: 123456789123, Desc: Desc1, Qty: 5 Price  $4.00
Item 2: UPC: 415678945612, Desc: Desc2, Qty: 2 Price  $2.15
Total: $100.00

What modification do I need to make to the script in order to produce the desired output?
0
Comment
Question by:100questions
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40400381
I would assume in the above example you want to see: "Ship to: LOCATION 1". If you want to strip off specific information please elaborate but for the whole N102 field where N101 = ST, try this code:
 Function ParseText ($OutputFile,$InputFile){
 Begin{
             $line,$poline,$shipto,$totline,$total,$i,$n3,$n4 = $null,$null,$null,$null,$null,0,0,0
             Set-Content $OutputFile $null
             $Data = Get-Content -Path $InputFile | ?{$_ -match "(G50\*)|(G69\*)|(G68\*)|(N\d\*)|(IEA\*)"}
       }
 Process{
       $Data | % {
       #Check the end of data set
             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$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 "G50\*" -and $_ -match "(\w{2}\*)(\d{3,10})"){
                   $poline = "PONumber: $($Matches[2])"
             }  
             #Collect the ST
             If($_ -match "N1\*(ST)\*([^*~]+)"){
                   $shipto = "`r`nShip to: $($Matches[2])"
             }  
             #Collect  G68 details and following G69 Description
             If($_ -match "(G68\*{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
 

Author Comment

by:100questions
ID: 40400479
You're right, I meant to say LOCATION 1.

I will try the code to see the results, thanks.
0
 

Author Closing Comment

by:100questions
ID: 40407025
Works very well, thank you.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 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 …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

9 Experts available now in Live!

Get 1:1 Help Now