Hello
I've done a quick example for your requirement, This is an example of a
regular expression pattern that uses the named submatch capability When the
expression is used with the -match operator, instead of using simple numeric
indexes the $matches for the substrings the variables will be used. You may
need to adjust for line breaks etc.
# example:
# $string = "Order No. 190805 Time= 02.50.47
Date= 10.25.2006"
#
# here is a Regular Expression string pattern to find and assign the order
number and order date to variables
# $pattern = [regex]
"^Order.*\.\s(?<OrderNumber>[0-9]{6})\s+[A-Za-z=]+(?<OrderTime>\d{1,2}[.]\d{1,2}[.]\d{1,2})\s+[A-Za-z=]+\s+(?<OrderDate>\d{1,2}[.]\d{1,2}[.]\d{4})"
#
# the contents of the substrings found are now contained in the powershell
special variable $matches
# i.e
# $matches.OrderNumber
# $matches.OrderTime
# $matches.OderDate
#
#=====================================================================
#example program
# assuming data formated as follows: "Order No. 190805
Date= 10.25.2006"
# The stings are contained in a text file
#
$infile = $null
$outfile = "c:\orderinfo.csv"
$Orderfile = "c:\rawdatafile.txt"
#create a record to hold the data while processing
function new-Order(){
$object = New-Object psobject |
add-Member -MemberType noteproperty -Name Number -Value 0 -PassThru |
add-Member -MemberType noteproperty -Name Time -Value 0 -PassThru |
add-Member -MemberType noteproperty -Name Date -Value 0 -PassThru
$object
}
$collection = @() # create an empty array to hold the collection of objects
(order data) to be exported
#look for ordernumber and orderdate in text file
$pattern = `
[regex]
"^Order.*\.\s(?<OrderNumber>[0-9]{6})\s+[A-Za-z=]+(?<OrderTime>\d{1,2}[.]\d{1,2}[.]\d{1,2})\s+[A-Za-z=]+\s+(?<OrderDate>\d{1,2}[.]\d{1,2}[.]\d{4})"
$infile = (Get-Content ($OrderFile) -ReadCount - 1)
for ($i = 0; $i -le $infile.length; $i ++ ){
if ($infile[$i] -match $pattern) {
$order = new-order
$order.Number = $matches.OrderNumber
$order.Time = $matches.OrderTime
$order.Date = $matches.OrderDate
$collection += $order
}
}
$collections | Export-Csv -Path $outfile -NoTypeInformation
=================
"Mícheál" <michael.halpin@xxxxxx> wrote in message
news:ac46d194-fc09-46a4-9d2d-3df501f092f5@xxxxxx
| Quote: |  | |
|
> Thanks for your help guys, here is pretty much what I've come up and
> it seems to work perfectly so far. Basically it just converts the file
> to one big string, splits it into one word per line and because the
> file is always the same layout its easy to do the offsets manually
> (the files are infact much bigger, but this is just the basic format).
> I look for a single distinct search string per line (from the
> original) but if thats not possible i just use some nested switch
> statements to get the desired result (for example I have readings for
> "zone 1", "zone 2", etc).
>
> ################################################
>
> dir awaiting\*.txt | foreach{
> $words = gc $_
> $words = [string]::join(" ", $words)
> $words = $words.split(" ",[stringsplitoptions]::RemoveEmptyEntries)
> $count = $words.count
>
>
> $index = 0
> while ($index -ne $count)
> {
> switch ($words[$index])
> {
> code{$ProdCode= $words[$index+1];$Prodcode}
> Time={$Time= $words[$index+1];$Time}
> order{$CO= $words[$index+2];$CO}
> Date={$Date= $words[$index+1];$Date}
> Zone{switch($words[$index+1])
> {
> 1{$TempZone1=$words[$index+3] ;$TempZone1}
> 2{$TempZone2=$words[$index+3] ;$TempZone2}
> 3{$TempZone3=$words[$index+3] ;$TempZone3}
> 4{$TempZone4=$words[$index+3] ;$TempZone4}
> }
> }
> $index++
> }
>
> }
| |
| | |