PowerShell odpowiednik grep-f
Szukam odpowiednika PowerShell do grep --file=filename
. Jeśli nie znasz grep
, filename jest plikiem tekstowym, w którym każda linia ma wzór wyrażenia regularnego, który chcesz dopasować.
Może brakuje mi czegoś oczywistego, ale Select-String
wydaje się, że nie ma takiej opcji.
7 answers
Parametr -Pattern
w Select-String
obsługuje tablicę wzorców. Więc ten, którego szukasz to:
Get-Content .\doc.txt | Select-String -Pattern (Get-Content .\regex.txt)
Przeszukuje plik tekstowy doc.txt
używając każdego wyrażenia regularnego(po jednym w linii) w regex.txt
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-04 12:06:58
PS) new-alias grep findstr
PS) C:\WINDOWS> ls | grep -I -N exe
105:-a--- 2006-11-02 13:34 49680 twunk_16.exe
106:-a--- 2006-11-02 13:34 31232 twunk_32.exe
109:-a--- 2006-09-18 23:43 256192 winhelp.exe
110:-a--- 2006-11-02 10:45 9216 winhlp32.exe
PS) grep /?
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-16 01:28:06
Nie znam grepa, ale z Select-String możesz zrobić:
Get-ChildItem filename.txt | Select-String -Pattern <regexPattern>
Możesz to również zrobić za pomocą Get-Content:
(Get-Content filename.txt) -match 'pattern'
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-04 11:28:45
Więc znalazłem całkiem dobrą odpowiedź pod tym linkiem: https://www.thomasmaurer.ch/2011/03/powershell-search-for-string-or-grep-for-powershell/
Ale zasadniczo jest to:
Select-String -Path "C:\file\Path\*.txt" -Pattern "^Enter REGEX Here$"
Daje to wyszukiwanie plików katalogowych (*lub możesz po prostu określić plik) i wyszukiwanie zawartości plików w jednej linii PowerShell, bardzo podobne do grep. Wynik będzie podobny do:
doc.txt:31: Enter REGEX Here
HelloWorld.txt:13: Enter REGEX Here
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-15 18:52:37
Ale Select-String nie ma takiej opcji.
Zgadza się. PowerShell jest nie klonem zestawu narzędzi *Nix shells.
Jednak nie jest trudno zbudować coś takiego samemu:
$regexes = Get-Content RegexFile.txt |
Foreach-Object { new-object System.Text.RegularExpressions.Regex $_ }
$fileList | Get-Content | Where-Object {
foreach ($r in $regexes) {
if ($r.IsMatch($_)) {
$true
break
}
}
$false
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-04 10:37:02
Może?
[regex]$regex = (get-content <regex file> |
foreach {
'(?:{0})' -f $_
}) -join '|'
Get-Content <filespec> -ReadCount 10000 |
foreach {
if ($_ -match $regex)
{
$true
break
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-04 12:07:30
To pytanie ma już odpowiedź, ale chcę tylko dodać, że w Windows istnieje Podsystem Windows dla Linuksa WSL .
Więc na przykład, jeśli chcesz sprawdzić, czy masz usługę o nazwie Elasicsearch która jest w stanie działa możesz zrobić coś takiego jak Poniższy fragment w powershell
net start | grep Elasticsearch
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-06 08:25:56