tags:

views:

37

answers:

3

A powershell question: I want to extract each line in a character stream produced by an application that matches a certain pattern which in pseudo-code would be something like this: PS> <a_ps_command> <the_application_command_for_outputting_the_text_stream> | <my_filter > output_file.txt

In my case the application is a CM-tool that outputs the change history of a source file and the (psuedo)pattern should be something like: <a couple of numbers><a name><a time stamp><a line of characters>

Cheers, Christian

+4  A: 

The filtering cmdlet in PowerShell is Where-Object (aliases Where and ?). You simply pass the output of the SCM command into it. You then use $_ to represent the current line and test against it e.g.:

tf hist .\Settings.StyleCop /i /stopafter:20 | Where {$_ -match '^\d+.*?Hack'}

The -Match operator is used to compare the current line of output against a regex. I use ^\d+ to filter out the first two lines of tf hist output (which are formatting strings) and then I search on the text Hack anywhere else on the line (looking for it in comments for instance). You would modify and enhance the regex to meet your needs.

Keith Hill
+1  A: 

You might want to look at Select-String. Powershell's answer to grep.

OldFart
+2  A: 

As a fan of operators I have to add third answer :]

You don't need to use cmdlets and piping. Just use operators like -match or -like:

PS> (ipconfig) -match 'ipv6'
PS> (ipconfig) -like '*ipv*'
stej