Solved

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

11 Experts available now in Live!

Get 1:1 Help Now