Solved

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

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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

717 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