Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - -match V1 vs V2

Reply
 
Old 08-31-2009   #1 (permalink)
Justin Rich


 
 

-match V1 vs V2

I noticed that -match works a lot differently in V1 vs V2. i was wondering
if there was some way to get the exact match (not line) of the regex that
would work in both V1 and V2.

im guessing no, since V1 uses $matches and V2 returns the results, but i
thought i'd ask

Thanks
Justin


My System SpecsSystem Spec
Old 08-31-2009   #2 (permalink)
Thomas Lee


 
 

Re: -match V1 vs V2

In message <B8F43321-48C0-456F-8687-28873D45FD36@xxxxxx>, Justin
Rich <jrich523@xxxxxx> writes
Quote:

>I noticed that -match works a lot differently in V1 vs V2. i was
>wondering if there was some way to get the exact match (not line) of
>the regex that would work in both V1 and V2.
>
>im guessing no, since V1 uses $matches and V2 returns the results, but
>i thought i'd ask
Can you provide a sample showing what you mean?
--
Thomas Lee
doctordns@xxxxxx
My System SpecsSystem Spec
Old 08-31-2009   #3 (permalink)
Shay Levy [MVP]


 
 

Re: -match V1 vs V2

Hi Justin,

Can you share the code you are using?


---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar



JR> I noticed that -match works a lot differently in V1 vs V2. i was
JR> wondering if there was some way to get the exact match (not line) of
JR> the regex that would work in both V1 and V2.
JR>
JR> im guessing no, since V1 uses $matches and V2 returns the results,
JR> but i thought i'd ask
JR>
JR> Thanks
JR> Justin


My System SpecsSystem Spec
Old 08-31-2009   #4 (permalink)
Justin Rich


 
 

Re: -match V1 vs V2

yeah i was just thinking i should do that, sorry

i have a text file that looks like:
****************************************************************
*
* Amicas database backup started at Fri 08/28/2009 22:08:01.32 on RADAMICAS2
*
****************************************************************

.... other info....

----------------------------------------------------------------
****************************************************************
*
* Amicas database backup completed at Sat 08/29/2009 19:12:20.25
*
****************************************************************

what i really want to do is grab the date/time and then do a a datediff to
figure out how much time it took.

so i did a pattern match like so "\d+/\d+/\d+\s\d+:\d+:\d+\.\d+" which finds
it, but in V2 it just returns the lines... in V1 it returns the lines and
filles $matches with the actual match.

i'd like a way to pull out the date and time that works in both V1 and V2

Thanks
Justin



"Thomas Lee" <tfl@xxxxxx> wrote in message
news:SQ5kgsY9Y+mKFAwa@xxxxxx
Quote:

> In message <B8F43321-48C0-456F-8687-28873D45FD36@xxxxxx>, Justin
> Rich <jrich523@xxxxxx> writes
Quote:

>>I noticed that -match works a lot differently in V1 vs V2. i was wondering
>>if there was some way to get the exact match (not line) of the regex that
>>would work in both V1 and V2.
>>
>>im guessing no, since V1 uses $matches and V2 returns the results, but i
>>thought i'd ask
>
> Can you provide a sample showing what you mean?
> --
> Thomas Lee
> doctordns@xxxxxx
My System SpecsSystem Spec
Old 08-31-2009   #5 (permalink)
Robert Robelo


 
 

Re: -match V1 vs V2

I'd recommend a switch statement to retrieve the start and end values.
The RegEx $pat will capture the date/time string, so when combined with the RegEx in the switch statement, this value will be available in the 1st group, i.e. $Matches[1]

# Requires -Version 2
$log = 'C:\Amicas.txt'
$pat = '(\p{L}{3} (\d{1,2}/){2}\d{4} (\d{1,2}\{2}\d{2}\.\d{2})'
[DateTime]$start, [DateTime]$end = switch -regex -file $log {
"^\* .+ started at $pat" {$Matches[1]}
"^\* .+ completed at $pat" {$Matches[1]}
}
$start
$end
$diff = $end - $start
$diff

--
Robert
My System SpecsSystem Spec
Old 08-31-2009   #6 (permalink)
Robert Robelo


 
 

Re: -match V1 vs V2

# you can reduce the switch statement to one pattern

[DateTime]$start, [DateTime]$end = switch -regex -file $log {
"^\* .+ (?:started|completed) at $pat" {$Matches[1]}
}

--
Robert
My System SpecsSystem Spec
Old 08-31-2009   #7 (permalink)
Justin Rich


 
 

Re: -match V1 vs V2

Thanks Robert. Part of what i was trying to do was make it compatible with
V1 and V2. currently my prod servers are all V1 but with V2 on the way im
trying to write any new scripts so that they work on either version.

Thanks
Justin

"Robert Robelo" <Kiron@xxxxxx> wrote in message
news:BB1E37E7-B48B-4A1D-BF92-30ACF5689C09@xxxxxx
Quote:

># you can reduce the switch statement to one pattern
>
> [DateTime]$start, [DateTime]$end = switch -regex -file $log {
> "^\* .+ (?:started|completed) at $pat" {$Matches[1]}
> }
>
> --
> Robert
My System SpecsSystem Spec
Old 08-31-2009   #8 (permalink)
Flowering Weeds


 
 

Re: -match V1 vs V2


Mmm since this is the year of data parsing
and charting (and yes Regex is also included)
then perhaps:

FYI

"Log Parser" - Bing
http://www.bing.com/search?q=%22Log+Parser%22

Mmm after all these years of examples
of Windows-based Log parser usage,
perhaps one is parsing the log or file with
Log Parser and storing the parsed data
as one goes along parsing the file or log

( moving data from tool to tool, is after all,
the PowerShell way )

and using this saved (file, array, etc.) parsed data
for perhaps a report for the boss?

Here PowerShell uses Log Parser:

$theDates = LogParser.exe "SELECT
EXTRACT_TOKEN(text,1,' at ')
FROM '$pwd\databaselog.log'
WHERE Text LIKE '* A%' " -i textline -q

# For later usage store the split up line.
$splitFirstLine = $theDates[0].Split(" ")

[DateTime]$start = $theDates[0].Split("on")[0]
[DateTime]$end = $theDates[1]
$start
$end
$diff = $end - $start
" "
$diff

# Perhaps make a report with a chart
# as well as text data for the boss.
$chartData = @"
Hours,Minutes,seconds
$($diff.hours),$($diff.minutes),$($diff.seconds)
"@

$chartTitle = $splitFirstLine[-1] + "`n" +
$splitFirstLine[0] + " " +
$splitFirstLine[1] + " to " +
$theDates[1].Split(" ")[0] + " " +
$theDates[1].Split(" ")[1]

$chartData | LogParser.exe "SELECT hours, minutes, seconds
into testChart.gif
FROM STDIN " -i csv -stats off -o chart `
-chartType ColumnClustered -chartTitle $ChartTitle

invoke-item testChart.gif

" "
"Done!"
" "
Exit

PowerShell 2 RTM
should make one want to include
all these exciting graphics
within one's boss's reports!

As always enjoy the automation
of tools, including graphic tools,
within the Windows-based, .NET aware,
WPF accessible, multi-processes on
the same IP / Port usage,
admin's automation tool,
powershell.exe!


My System SpecsSystem Spec
Old 08-31-2009   #9 (permalink)
Justin Rich


 
 

Re: -match V1 vs V2

actually, what im doing is pulling in all this data about different jobs
(backups mostly) and dumping it in to a DB to do trending on and i was going
to use log parser (or more likely the web graph addon that it uses) to
create web pages that will show this sort of info... increase of time and
size etc etc

Thanks!
Justin

"Flowering Weeds" <no@xxxxxx> wrote in message
news:utqiSimKKHA.1252@xxxxxx
Quote:

>
> Mmm since this is the year of data parsing
> and charting (and yes Regex is also included)
> then perhaps:
>
> FYI
>
> "Log Parser" - Bing
> http://www.bing.com/search?q=%22Log+Parser%22
>
> Mmm after all these years of examples of Windows-based Log parser usage,
> perhaps one is parsing the log or file with Log Parser and storing the
> parsed data as one goes along parsing the file or log
>
> ( moving data from tool to tool, is after all,
> the PowerShell way )
>
> and using this saved (file, array, etc.) parsed data for perhaps a report
> for the boss?
>
> Here PowerShell uses Log Parser:
>
> $theDates = LogParser.exe "SELECT
> EXTRACT_TOKEN(text,1,' at ')
> FROM '$pwd\databaselog.log'
> WHERE Text LIKE '* A%' " -i textline -q
>
> # For later usage store the split up line.
> $splitFirstLine = $theDates[0].Split(" ")
>
> [DateTime]$start = $theDates[0].Split("on")[0]
> [DateTime]$end = $theDates[1]
> $start
> $end
> $diff = $end - $start
> " "
> $diff
>
> # Perhaps make a report with a chart
> # as well as text data for the boss.
> $chartData = @"
> Hours,Minutes,seconds
> $($diff.hours),$($diff.minutes),$($diff.seconds)
> "@
>
> $chartTitle = $splitFirstLine[-1] + "`n" +
> $splitFirstLine[0] + " " +
> $splitFirstLine[1] + " to " +
> $theDates[1].Split(" ")[0] + " " +
> $theDates[1].Split(" ")[1]
>
> $chartData | LogParser.exe "SELECT hours, minutes, seconds
> into testChart.gif
> FROM STDIN " -i csv -stats off -o chart `
> -chartType ColumnClustered -chartTitle $ChartTitle
>
> invoke-item testChart.gif
>
> " "
> "Done!"
> " "
> Exit
>
> PowerShell 2 RTM
> should make one want to include
> all these exciting graphics
> within one's boss's reports!
>
> As always enjoy the automation
> of tools, including graphic tools,
> within the Windows-based, .NET aware,
> WPF accessible, multi-processes on
> the same IP / Port usage,
> admin's automation tool,
> powershell.exe!
>
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
match question PowerShell
CD key does not match disk? Vista installation & setup
Key does no match disk?!? Vista installation & setup
Files do not match Vista hardware & devices
Product key does not match... Vista installation & setup


Vista Forums 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.
© Designer Media Ltd

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