Prints character offset before each matching line. Prints only the file name if a file contains a match. Prints the line number of each line that matches. Prints only lines that don't contain a match. Ignores the case of the characters when searching for the string. Searches the current directory and all subdirectories. Processes search strings as regular expressions. Matches the text pattern if it is at the end of a line. Matches the text pattern if it is at the beginning of a line. Of course, various functions exist that enable you to modify the found substrings.Searches for patterns of text in files. One example would be to format the output in a much easier-to-read layout by piping the result into Format-List or Out-GridView. String processing of matchesīecause you can pass the result of Select-String to a variety of cmdlets, you have almost unlimited possibilities to process the results. To get all matches, you have to add the -AllMatches switch. The example below shows the exact matches: Select-String -Path *.* -Pattern "DO.*=" | Select Matchesīy default, you only see the first match in each line. With complex regular expressions, you are often uncertain about the strings they actually match. If you work with regular expressions, Matches is another particularly helpful property. (The parameter -Context also displays lines before and after the line with the match.)įor example, the command below only displays filenames and lines that contain the search string: Select-String -Path index.html -SimpleMatch "Home" | Select FileName, LineNumber Usage of MatchInfo propertiesįor instance, properties are useful for changing the output of the cmdlet if you are unhappy with the relatively confusing presentation of path, filename, and line number of Select-String. If you pass the output of Select-String to Get-Member, you’ll receive a list of all methods and properties. Compared to flat text, objects have the advantage of offering methods and properties that allow you to process their output in a more sophisticated way. Search results as MatchInfo objectsĪs opposed to text-oriented tools such as find, a cmdlet returns an object (in the case of Select-String of the type MatchInfo). However, you can accomplish this by piping the output of the Get-ChildItem cmdlet into Select-String: Get-ChildItem *.* -Exclude *.pdf -Recurse | Select-String -Pattern "DO.*="īecause Get-ChildItem selects the files, Select-String only has to take care of the search pattern. In contrast to find and findstr, Select-String cannot search recursively in subdirectories. In the above example, Select-String searches only in files in the current directory, excluding ZIP archives and PDF files. You may further restrict your search by excluding files with the parameter -Path, together with wildcards: Select-String -Path *.* -Exclude *.pdf,*.zip -Pattern "DO.*=" -NotMatch Search in subdirectories with Get-ChildItem With -NotMatch, PowerShell’s grep counterpart Select-String also supports reverse search, which finds only lines that don’t contain the search pattern. For a simple text search in a file, you can use the -SimpleMatch parameter instead: Select-String -Path index.html -SimpleMatch "Home" Reverse search with -NotMatch The -Pattern parameter is reserved for the search with regular expressions. In PowerShell, it is just the other way around: the more uncommon case-sensitive search has to be activated explicitly with the -CaseSensitive switch. The comparison is case-insensitive, which is an advantage compared to the old tools where you always have to add the parameter /i. This command returns all lines that contain the search string, including the line numbers. In the simplest usage scenario, you just have to pass a filename (or, with the help of wildcards, a pattern of filenames) and the search string: Select-String -Path index.html -Pattern "Home" In addition, it offers the common advantages of a cmdlet that is well integrated in PowerShell. However, PowerShell goes beyond the capabilities of those tools, among others, because of a full implementation of regular expressions.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |