Groups > Microsoft > Windows PowerShell > Re: Parsing Text File and returning variables




Parsing Text File and returning variables

Parsing Text File and returning variables
Fri, 11 Apr 2008 03:43:27 -070
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

Post Reply
Re: Parsing Text File and returning variables
Fri, 11 Apr 2008 09:05:35 -030
> #################################
> 
> 
> 
> 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:
Post Reply
Re: Parsing Text File and returning variables
Fri, 11 Apr 2008 13:32:44 +000
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> 

Post Reply
Re: Parsing Text File and returning variables
Mon, 14 Apr 2008 07:28:17 -070
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++
	}

Post Reply
about | contact