grep, Windows PowerShell Style
Recently, when the Linux cluster that we were working on, was taken down for maintenance, we were wondering how to search for a string in the code. We wanted to find all the code files that refer to a particular parameter. In other words, we wanted to execute grep.
While I know that Windows PowerShell is more feature rich as compared to DOS/Windows Batch, the syntax means it takes some time to get things working. But the cluster not being available, made me look at PowerShell. After a search and some trial and error here is the command sequence I used
Select-String -Pattern '^\s*#\s*query' -NotMatch -Path './<dir>/*.*' | Select-String -Pattern 'query' | Select-String -Pattern '\b<text>\b'
This set of commands searches for the a string name in the directory, while ensuring that the text has a specific pattern, and also ignoring the same pattern if it begins with a '#' (comment). The '\b' ensures that the text is whole words.
#windows #powershell #grep