Solved

I need a Powershell script which will show the PO numbers ?

Posted on 2014-10-27
2
103 Views
Last Modified: 2014-10-28
Input file similar to this:

BEG*00*SA*1234567894**20141027~
BEG*00*SA*2345678945**20141027~
BEG*00*SA*4567894564**20141027~
BEG*00*SA*9876543222**20141027~
BEG*00*SA*7524587598**20141027~
G50*N*20141027*123456~
BEG*00*NE*12345**20141027~
BEG*00*NE*12344**20141027~


Output file, must show the following:

PO: 1234567894
PO: 2345678945
PO: 4567894564
PO: 9876543222
PO: 7524587598
PO: 12345
PO: 12344

For each Beg segment line which is found it must output the PO number shown immediately after the SA segment
For each G50 segment line which is found it must output the PO number which is the after the date
For each BEG segment line which is found, and if an NE is found,  it must output the PO number which is right after the NE
0
Comment
Question by:100questions
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40407656
Well since you don't need to check for logical document boundaries here you can use a much simpler version of the script you've been using thus far:
$InputFile = "C:\Data\Original.txt"
$OutputFile = "C:\Data\New.txt"

Set-Content $OutputFile $null
If(Test-Path $InputFile){
    Get-Content -Path $InputFile | ?{$_ -match "(^BEG\*)|(^G50\*)"} | %{
        #Collect the PO Numbers
        If($_ -match "^BEG\*\d*\*(SA|NE)\*(\d{3,10})"){
            "PO: $($Matches[2])"
        }
        If($_ -match "^G50\*\w*\*(\d{8})\*(\d{3,10})"){
            "PO: $($Matches[2])"
        }
    } | out-file $OutputFile -Encoding UTF8
}

Open in new window

I've taken the liberty of removing the use of a function but it should be easy to add back in if there was a specific reason for doing it like that.

Also note that I have added ^ in the regular expressions, which means it should start at the beginning of a line (this should improve speed and eliminate the slight chance of false positives). On a previous question you have posted an example document that had spaces at the start of lines but I don't suppose those will normally be in these documents?
0
 

Author Comment

by:100questions
ID: 40408478
Probably the spaces won't appear at the beginning of the lines.  Thanks for noticing that.
The script works very well, thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
A brief introduction to what I consider to be the best editor for PowerShell.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

929 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

14 Experts available now in Live!

Get 1:1 Help Now