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 - Suppressing file name and line number when using Select-String

Reply
 
Old 10-08-2009   #1 (permalink)
Al Fansome


 
 

Suppressing file name and line number when using Select-String

Anyone have a simple way of suppressing the output of the file name and
line number when using Select-String on a file?

My System SpecsSystem Spec
Old 10-08-2009   #2 (permalink)
Sean Kearney


 
 

RE: Suppressing file name and line number when using Select-String

Filename dog.txt searching for 'cat'

select-string -path dog.txt -pattern 'cat'

To NOT get the excess information, choose what you want

foreach ($line in ( select-string -path dog.txt -pattern cat ) ) {
$line.line }

Maybe not the BEST way but these will show you only the "line" subvariable.
You can get what the command is REALLY containing by doing a GET-MEMBER
against the contents so doing it like this

select-string -path dog.txt -pattern cat | get-member

Will show you the following output

TypeName: Microsoft.PowerShell.Commands.MatchInfo

Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
RelativePath Method string RelativePath(string directory)
ToString Method string ToString(), string ToString(string directory)
Context Property Microsoft.PowerShell.Commands.MatchInfoContext
Context {get;set;}
Filename Property System.String Filename {get;}
IgnoreCase Property System.Boolean IgnoreCase {get;set;}
Line Property System.String Line {get;set;}
LineNumber Property System.Int32 LineNumber {get;set;}
Matches Property System.Text.RegularExpressions.Match[] Matches
{get;set;}
Path Property System.String Path {get;set;}
Pattern Property System.String Pattern {get;set;}

The lines with a PROPERTY beside them are something you would see on the
screen. Normally with that command you're seeing LINENUMBER and LINE. But
you can get Powershell to show you only what you want.

Hope this helps

"Al Fansome" wrote:
Quote:

> Anyone have a simple way of suppressing the output of the file name and
> line number when using Select-String on a file?
>
My System SpecsSystem Spec
Old 10-09-2009   #3 (permalink)
Larry__Weiss


 
 

Re: Suppressing file name and line number when using Select-String

select-string -Path thefilename "thepattern" | ForEach-Object {$_.Line}

and going in the opposite direction, to see all of the MatchInfo fields, use
select-string -Path thefilename "thepattern" | fl

- Larry

Al Fansome wrote:
Quote:

> Anyone have a simple way of suppressing the output of the file name and
> line number when using Select-String on a file?
>
My System SpecsSystem Spec
Old 10-30-2009   #4 (permalink)
Al Fansome


 
 

Re: Suppressing file name and line number when using Select-String

Here's an even simpler way:

cat file.ext | ss "pattern"

I believe this works because cat returns strings, rather than match
objects, and ss just prints out the string if there is a match. If you do

ss "pattern" file.ext

you get match objects, and you have to extract just the info you need
from the match objects to eliminate the filename and the line number.
This adds complexity to the command line.

As an added bonus, cat apparently returns ASCII strings rather than
Unicode, so

cat file.ext | grep "pattern"

works, where grep is GNU grep. grep can't handle Unicode, so

grep "pattern" file.ext

will fail on Unicode files.

Al Fansome wrote:
Quote:

> Anyone have a simple way of suppressing the output of the file name and
> line number when using Select-String on a file?
My System SpecsSystem Spec
Old 10-30-2009   #5 (permalink)
Larry__Weiss


 
 

Re: Suppressing file name and line number when using Select-String

ASCII encoding is not the default for Get-Content (aka cat), but is the
side-effect of piping to an executable.

From
http://blogs.msdn.com/powershell/arc...he-rescue.aspx
"The reason we convert to ASCII when piping to existing executables
is that most commands today do not process UNICODE correctly.
Some do, most don't." - Jeffrey Snover

- Larry


Al Fansome wrote:
Quote:

> Here's an even simpler way:
>
> cat file.ext | ss "pattern"
>
> I believe this works because cat returns strings, rather than match
> objects, and ss just prints out the string if there is a match. If you do
>
> ss "pattern" file.ext
>
> you get match objects, and you have to extract just the info you need
> from the match objects to eliminate the filename and the line number.
> This adds complexity to the command line.
>
> As an added bonus, cat apparently returns ASCII strings rather than
> Unicode, so
>
> cat file.ext | grep "pattern"
>
> works, where grep is GNU grep. grep can't handle Unicode, so
>
> grep "pattern" file.ext
>
> will fail on Unicode files.
>
> Al Fansome wrote:
Quote:

>> Anyone have a simple way of suppressing the output of the file name
>> and line number when using Select-String on a file?
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Need full line of text from select-string PowerShell
The best way to get number of string occurrences in a binary file PowerShell
problems with $var | select-string -pattern $string -q PowerShell
Search for string in CSV and delete line if string found in line PowerShell
How to display the line number from -switch -regex -file? PowerShell


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