?
Solved

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

Posted on 2014-10-27
2
Medium Priority
?
117 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

770 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