![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||