Команда grep
в Linux широко используется для парсинга файлов и поиска полезных данных в выводах различных команд.
В командной строке Windows (CMD) эквивалентом grep
является команда findstr
.
В Windows PowerShell эквивалентом команды grep
выступает команда Select-String
.
Ниже вы найдете несколько примеров того, как использовать эти аналоги команды grep
в Windows.
Команда Grep в Windows
Отфильтровать вывод команды netstat
по определенному порту:
# Windows CMD C:\> netstat -na | findstr "PORT" # Windows PowerShell PS C:\> netstat -na | Select-String "PORT"
Если команда в PowerShell возвращает некие объекты, то перед парсингом они должны быть преобразованы в строки с помощью команды Out-String -Stream
:
# Windows PowerShell PS C:\> Get-Alias | Out-String -Stream | Select-String "curl"
Напечатать строки файла, которые соответствуют регулярному выражению (независимо от регистра):
# Windows CMD C:\> findstr /i "^SEARCH.*STRING$" file.txt # Windows PowerShell PS C:\> Select-String "^SEARCH.*STRING$" file.txt
Вывести help
для аналогов команды grep
в Windows:
# Windows CMD C:\> findstr /? # Windows PowerShell PS C:\> get-help Select-String
Hi. I tried to type in the cmd prompt
` C:\> netstat -na | findstr «2020»` and enter, and it doesn’t work. I want to search in directories of word document some strings. It seems that I missed something in your explanations
What you want is this section of the article:
# Windows CMD
C:\> findstr /i «^SEARCH.*STRING$» file.txt
In the example above you can change «^SEARCH.*STRING$» to «2020». i.e.
C:\> findstr /i «2020» *.txt
The above command will search for «2020» in all txt files in your current directory.
Great write-up. I found exactly what I needed.
One correction, though, is in invoking findstr’s help, which follows the customary form of other Windows commands: findstr /?
Thanks!
Fixed. Thanks.
PS C:\Users\Administrator> auditpol.exe /get /category:* | findstr «Directory Service Replication»
Certification Services No Auditing
Directory Service Changes No Auditing
Directory Service Replication No Auditing
Detailed Directory Service Replication No Auditing
Directory Service Access No Auditing
Kerberos Service Ticket Operations No Auditing
Kerberos Authentication Service No Auditing
i want exact match plz help me its urgent
use find instead of findstr (findstr will return all matches Directory or Service or Replication, find will return a match of the entire string (case sensitive). Use find -i to ignore case.
i want the one output that one exact match word to word
auditpol.exe /get /category:* | findstr /C:»Directory Service Replication»
Would you know why the Select-String command in PowerShell returns just the pattern match vs. findstr returning the entire line like grep would?
For example:
If I use Set-Alias -NAME grep -Value Select-String
‘Get-Alias |Select-String grep’ will return:
grep
‘Get-Alias |findstr grep’ will return:
Alias grep -> Select-String
Is there a way to get the same output of Select-String sofor the sake of using PowerShell commands only?
It is the behavior of the get-alias command that is giving the trouble.
Turn the output into strings first:
‘Get-Alias | Out-String -stream | Select-String grep’