我正在尝试使用Powershell(版本4)从Windows上的一组文件中提取文本:
PS > Select-String -AllMatches -Pattern <mypattern-with(capture)> -Path file.jsp | Format-Table
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.这给出了一组很好的MatchInfo对象:
IgnoreCase LineNumber Line Filename Pattern Matches
---------- ---------- ---- -------- ------- -------
True 30 ... file.jsp ... {...}
Run Code Online (Sandbox Code Playgroud)
接下来,我看到捕获是在匹配成员中,所以我把它们拿出来:
PS > Select-String -AllMatches -Pattern <mypattern-with(capture)> -Path file.jsp | ForEach-Object -MemberName Matches | Format-Table
Run Code Online (Sandbox Code Playgroud)
这使:
Groups Success Captures Index Length Value
------ ------- -------- ----- ------ -----
{...} True {...} 49 47 ...
Run Code Online (Sandbox Code Playgroud)
或作为列表| Format-List:
Groups : {matched text, captured group}
Success : True
Captures …Run Code Online (Sandbox Code Playgroud) 我有线 -
echo $LocalAdmins
Administrator
Domain-Administrator
daemon
SomeUser
Run Code Online (Sandbox Code Playgroud)
第1-3行应该是相同的,尽管可能存在域管理员不存在的情况,因此简单地计算行将无济于事,因为用户可能隐藏在第3行.
第4行字符串更改,可能有4行以上,也没有第4行.
我不确定线路是否改变了位置.
我需要获取字符串-notMatch"Administrator","Domain-Administrator","daemon".即,我需要此列表中的用户名.
有没有办法使用多个-notMatch -and或()?目前我坚持只使用一个-notMatch的代码.我不能使用-Match,因为列表中可能有2个以上的用户.
$LocalAdmins | Select-String -pattern "Administrator" -notMatch
Run Code Online (Sandbox Code Playgroud) 我是powershell的初学者,我怀疑这将是一个简单的问题.我正在尝试执行以下命令,但它不返回任何结果,我不明白为什么.
我正在尝试获取bcdedit当前部分的描述.如果我做:
bcdedit /enum | select-string "identifier.*current" -context 0,3
Run Code Online (Sandbox Code Playgroud)
它返回以下内容:
> identifier {current}
device partition=C:
path \WINDOWS\system32\winload.exe
description Windows 8.1
Run Code Online (Sandbox Code Playgroud)
那么为什么以下不归description Windows 8.1呢?
bcdedit /enum | select-string "identifier.*current" -context 0,3 | select-string "description"
Run Code Online (Sandbox Code Playgroud)
相反,它根本不返回任何东西.
任何有关这方面的信息将不胜感激.
我试图在文件夹中的所有文件中提取以"%%"开头的每一行,然后将这些行复制到单独的文本文件中.目前在PowerShell代码中使用此代码,但我没有得到任何结果.
$files = Get-ChildItem "folder" -Filter *.txt
foreach ($file in $files)
{
if ($_ -like "*%%*")
{
Set-Content "Output.txt"
}
}
Run Code Online (Sandbox Code Playgroud) 我希望在 Powershell中Select-String考虑\r\n(回车+换行)一行的结尾。
但是,如下所示,abc匹配整个整个输入:
PS C:\Tools\hashcat> "abc`r`ndef" | Select-String -Pattern "abc"
abc
def
Run Code Online (Sandbox Code Playgroud)
如果我将字符串分成两部分,则Select-String表现如我所料:
PS C:\Tools\hashcat> "abc", "def" | Select-String -Pattern "abc"
abc
Run Code Online (Sandbox Code Playgroud)
我怎样才能给出Select-String一个以 结尾的字符串\r\n,然后让这个 cmdlet 只返回那些包含匹配的字符串?
我在一个目录中搜索模式,开关是-SimpleMatch -List.但它返回一个文件列表.怎么做只返回第一个文件和第一行?
Select-String在PowerShell中使用以下命令:
Select-String -Path E:\Documents\combined0.txt -Pattern "GET /ccsetup\.exe" -AllMatches > E:\Documents\combined3.txt
Run Code Online (Sandbox Code Playgroud)
创建一个输出文件,每行以路径和文件名开头,后跟冒号.例如:
E:\Documents\combined0.txt:255:255.255.255 - - [31/Dec/2014:04:15:16 -0800] "GET /ccsetup.exe HTTP/1.1" 301 451 "-" "Mozilla/5.0 (compatible; SearchmetricsBot; http://www.xxxx.com/en/xxxx-bot/)"
Run Code Online (Sandbox Code Playgroud)
如何在结果中删除输出文件路径名,输出文件名和冒号?
我正在尝试Select-String使用多行文本。
例子:
"This is line1
<Test>Testing Line 2</Test>
<Test>Testing Line 3</Test>"
Run Code Online (Sandbox Code Playgroud)
我希望能够在Select-String. 但是,它仅在我执行时选择第一行Select-String -Pattern "Line1"。如何将所有 3 行提取在一起?
Select-String -Pattern "Line1"
Run Code Online (Sandbox Code Playgroud) 我试图编写一个简单的PS脚本来检查大型.txt日志文件中的短字符串:“ SRVE0242I:”
$lines = Select-String -Path $logDir -Pattern "SRVE0242I:" | Select-Object line | Out-String
Run Code Online (Sandbox Code Playgroud)
但是在输出时,它仅显示以下内容:
行
[28/06/17 13:48:27:839] 00000020 ServletWrappe I SRVE0242I:[User] [User] [com_xxxxxxx _...
而不是全线。提取的字符数是否有限制?我找不到有关Select-String cmdlet的任何限制的任何信息。有没有更好的方法可以做到这一点,所以我不会a)在行列表中拉标题“ Line”(不想真的为这种简单的输出创建表格格式),b)获得整行当我提取信息时?
我有一个生成的报告文件,并包含各种文件引用.我正在使用Select-String和正则表达式匹配某些类型的文件并对它们执行后续处理.
我遇到的困境是在零(0),一(1)或多于一(2+)次匹配时始终确定匹配的数量.这是我尝试过的:
(select-string -path $outputfilePath -pattern $regex -allmatches).matches.count
Run Code Online (Sandbox Code Playgroud)
如果有0个匹配则返回"null",如果匹配则返回"1",如果多个匹配则返回"null".
(select-string -path $outputfilePath -pattern $regex -allmatches).count
Run Code Online (Sandbox Code Playgroud)
如果存在0或1匹配则返回"null",如果匹配多于匹配则返回匹配数.
我对Powershell很新,但我试图找到一种一致的方法来测试匹配的数量,无论是否有0,1或多于1的匹配.