Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums

Go Back   Vista Forums > Vista technology newsgroups > PowerShell

Parsing Text File and returning variables

Reply
 
Thread Tools Display Modes
Old 04-11-2008   #1
Mícheál
Guest
 
Posts: n/a

Parsing Text File and returning variables

Hey, I need help with a problem I have working on a text file.
Basically what I want to do is find a way to search for a line
expression, and then pull out the next numeric value into a variable
to be stored in the script (I will then take all these variables and
upload them to a sql db).

in other words, for the example below, I would like to search for
"product code" and then assign 7618 to a $ProdCode, find time and set
$TimeStamp = "02.05.47", etc.
I'm sure there's an easy way, I just can't figure it out!



#################################



Product Code 7618 Time=
02.05.47


Order No. 190805 Date=
10.25.2006

D a t a P r i n t o u t


Line Speed ........... 13.6 M/min


Roll Speed ................. 13.4 M/min


  Reply With Quote

Old 04-11-2008   #2
Marco Shaw [MVP]
Guest
 
Posts: n/a

Re: Parsing Text File and returning variables

Quote:

> #################################
>
>
>
> Product Code 7618 Time=
> 02.05.47
>
>
> Order No. 190805 Date=
> 10.25.2006
>
> D a t a P r i n t o u t
>
>
> Line Speed ........... 13.6 M/min
>
>
> Roll Speed ................. 13.4 M/min
>
>
Is that all you would have in this file? So you're trying to search
through *multiple files* or would there be several of these types of
entries in *one* file?

Are the above lines more or less on the same line or multiple?

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
  Reply With Quote
Old 04-11-2008   #3
Brandon Shell [MVP]
Guest
 
Posts: n/a

Re: Parsing Text File and returning variables

Take a look at this entry http://bsonposh.com/modules/wordpress/?p=59

I did something very similar to what your looking for.

Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject

M> Hey, I need help with a problem I have working on a text file.
M> Basically what I want to do is find a way to search for a line
M> expression, and then pull out the next numeric value into a variable
M> to be stored in the script (I will then take all these variables and
M> upload them to a sql db).
M>
M> in other words, for the example below, I would like to search for
M> "product code" and then assign 7618 to a $ProdCode, find time and set
M> $TimeStamp = "02.05.47", etc.
M> I'm sure there's an easy way, I just can't figure it out!
M> #################################
M>
M> Product Code 7618 Time=
M> 02.05.47
M>
M> Order No. 190805 Date=
M> 10.25.2006
M>
M> D a t a P r i n t o u t
M>
M> Line Speed ........... 13.6 M/min
M>
M> Roll Speed ................. 13.4 M/min
M>


  Reply With Quote
Old 04-14-2008   #4
Mícheál
Guest
 
Posts: n/a

Re: Parsing Text File and returning variables

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++
}

}
  Reply With Quote
Old 04-18-2008   #5
B Williams
Guest
 
Posts: n/a

Re: Parsing Text File and returning variables

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++
> }
>
> }
  Reply With Quote
 
Reply

Thread Tools
Display Modes









Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Vistax64.com 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48