All in all, it was a Seattle Chamber of Commerce type of day. The day I flew into Seattle was absolutely beautiful, and I was able to see all the mountains stretching up into the sky. We even had a few sun sightings during the week. The TechReady conference in Seattle last week was great. It seems that all these commands return data, and I would love to be able to easily parse the information that the commands return. Hey, Scripting Guy! I have enjoyed reading Sean’s legacy scripting articles, but I am a bit confused. Highlighting the match will make the job easier.Summary: In this article, Microsoft Scripting Guy Ed Wilson teaches how to use the Windows PowerShell version of grep to parse the command line. In a result where there are a lot of match and text seeking the match specifically very hard. PS> Select-String -Pattern "case" -Context 1,2 poet.txt Display N Line Before and After Match Highlighting Matches In this example we will print 1 lines before match and 2 lines after match in a single shot. Now we will provide both parameters to the -Context where before and after line numbers will be provided. PS> Select-String -Pattern "case" -Context 0,3 poet.txt Display N Lines After Match Display N Line Before and After Match In this example we will print 3 lines after the match. We will use -Context options again but we will provide after part of the line numbers. PS> Select-String -Pattern "case" -Context 2,0 poet.txt Display N Lines Before Match Display N Lines After Match 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. 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 need to match whole word which is surrounded by white spaces we should put white spaces around the search term. 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. In this example we will use regular expression E.*E to match string. We can provide regular expressions into pattern too. Select-String command also supports regular expressions. Regular expression provides to define more rich and structured string expressions. PS> Select-String -Pattern EX -CaseSensitive *.txt Case Sensitive Search Match Regular Expression We can change this behaviour by using -CaseSensitve option 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. We will provide Get-ChildItem command to provide files recursively to the Select-String command like below. Recursively searching will look given string in all current folder and al subfolders. Now the most advanced file specification is searching files recursively. 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. We can search string in multiple files by providing file name or extension with the help asterisk. In previous example we have searched given string in a single file but real world problems are more than that. \poet.txt Search String In A File Search String In Multiple Files
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |