Solved

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

Posted on 2014-10-27
2
109 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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