![]() Show two lines before and after each match of the word “exception”: Select-String -Path C:logsexample.log -Pattern "exception" -Context 2 Displaying Lines Before and After the Match Search for the word “success” across all log files in a directory: Select-String -Path C:logs*.log -Pattern "success"ĥ. Search for any email addresses in a text file: Select-String -Path C:documentscontacts.txt -Pattern Searching Across Multiple Files Search for the word “Error” with exact casing in a log file: Select-String -Path C:logsexample.log -Pattern "Error" -CaseSensitive Linux provides tool named grep for filter text data or output according to given string or regular expression.Search for the word “error” in a log file: Select-String -Path C:logsexample.log -Pattern "error" This tool is popular amongst Linux system administrators. On the other side Windows operating systems generally lacks this tool and its functionality up to Powershell. Powershell provides Select-String commandlet to provide similar features and options the Linux grep tool provides. In this tutorial we will look different use cases with examples of Select-String tool. PS> get-help Select-String Help Search String In A File Help about Select-String can be get with the following command. \poet.txt Search String In A File Search String In Multiple Files -Pattern specifies the string we are searching for.One of the simplest usage and most used feature is simply searching given string in a file. ![]() In previous example we have searched given string in a single file but real world problems are more than that. We can search string in multiple files by providing file name or extension with the help asterisk. PS> Select-String -Pattern EX *.txt Search String In Multiple Files Search Files Recursively In this example we will search in all text files by specifying *.txt file name. Now the most advanced file specification is searching files recursively. Recursively searching will look given string in all current folder and al subfolders. We will provide Get-ChildItem command to provide files recursively to the Select-String command like below. PS> Get-ChildItem c:\*.txt -Recurse | Select-String -Pattern EX Search Files Recursively Case Sensitive Searchīy default given strings are searched case insensitive. ![]() PS> Select-String -Pattern EX -CaseSensitive *.txt Case Sensitive Search Match Regular Expression We can change this behaviour by using -CaseSensitve option like below. Regular expression provides to define more rich and structured string expressions. ![]() Select-String command also supports regular expressions. We can provide regular expressions into pattern too. In this example we will use regular expression E.*E to match string. PS> Select-String -Pattern "EX.*E" poet.txt Match Regular Expression Match Whole Wordīy default given search term or string is looked partially or on whole words. If we need to match whole word which is surrounded by white spaces we should put white spaces around the search term. PS> Select-String -Pattern " case " poet.txt Match Whole Word Display N Lines Before Match We will search case search term as a whole word. If we are looking some part of the text and need to see previous lines of the matches we can provide -Context option whit the number of lines we want to print. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |