我需要在多行字符串变量的匹配之前返回一行.
当输入使用字符串变量时,Select-String似乎认为整个字符串已匹配.因此,Context属性在字符串的两端"外部"并且为null.
考虑以下示例:
$teststring = @"
line1
line2
line3
line4
line5
"@
Write-Host "Line Count:" ($teststring | Measure-Object -Line).Lines #verify PowerShell does regard input as a multi-line string (it does)
Select-String -Pattern "line3" -InputObject $teststring -AllMatches -Context 1,0 | % {
$_.Matches.Value #this prints the exact match
$_.Context #output shows all context properties to be empty
$_.Context.PreContext[0] #this would ideally output first line before the match
$_.Context.PreContext[0] -eq $null #but instead is null
}
Run Code Online (Sandbox Code Playgroud)
我在这里误解了什么吗?
匹配"line3"时返回"line2"的最佳方法是什么?
谢谢!
编辑:我忽略的附加要求:需要在所有匹配的行上方提供一行不确定长度的行.EG在下面搜索"line3"时我需要返回"line2"和"line5".
line1
line2 …Run Code Online (Sandbox Code Playgroud) 我在 Powershell 脚本中有这一行:
if (Select-String -Path "$holidays" -Pattern "(?<!\*)$datestr" -SimpleMatch -Quiet)
$holidays 是一个文本文件,其中我有一些日期:
2023-09-04
2023-11-23
*2023-11-24
2023-12-25
Run Code Online (Sandbox Code Playgroud)
$datestr 是今天日期:2023-11-23
为什么负向后看模式在上面的行中不起作用?
我只是想排除(不匹配)所有以 * 开头的日期
我认为负向回顾中有问题,因为该行:
if (Select-String -Path "$holidays" -Pattern $datestr -SimpleMatch -Quiet)
Run Code Online (Sandbox Code Playgroud)
工作完美 --> 结果是正确的
第一行结果为 false
我正在尝试在目录c:\bats\中搜索包含unc路径的批处理文件\\server\public
命令:
Get-ChildItem -path c:\bats\ -recurse | Select-string -pattern "\\server\public"
Run Code Online (Sandbox Code Playgroud)
我收到与字符串相关的错误\\server\public:
Select-string : The string \\server\public is not a valid regular
expression: parsing "\\server\public" - Malformed \p{X} character
escape. At line:1 char:91
+ ... ts" -recurse | Select-string -pattern \\server\public
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Select-String], ArgumentException
+ FullyQualifiedErrorId : InvalidRegex,Microsoft.PowerShell.Commands.SelectStringCommand
Run Code Online (Sandbox Code Playgroud)
我一直在使用各种转义如试图"\server\public"或"'\server\public'"但我总是收到同样的错误.
我正在构建一个脚本,该脚本将访问大量文件并拉出特定字符串以便分配为变量。
所有文件中的字符串都是相似的,这不是问题。我能够使这个过程作为一个单独的事件运行(定义一个源文件)
$hostname_import = select-string .\test.txt -Pattern 'hostname ABC-.+'
$hostname = $hostname_import -replace '.+ ',''
Run Code Online (Sandbox Code Playgroud)
上面将输出目标文件中标识的特定主机名(第二个功能是修剪单词主机名和空格),然后我可以使用它继续根据需要使用变量来执行各种操作。
我遇到的问题是在 foreach 循环中执行此函数,以便我可以获取初始文件 - 执行选择字符串函数(或类似函数),然后按预期在循环内执行数据修改。
对于上下文 - 我正在查看配置文件 - 并根据这些配置构建一个单独的文件来报告结果 - 报告构建的一部分需要设备的主机名。
--编辑 1: 在咨询了我的橡皮鸭之后,我将尝试将这些文件作为 CSV 导入,以便可能找到解决方案。
巨大的帮助!
这很简单......为什么第一个命令工作而第二个没有?
Findstr看起来最好用在"dos"之类的命令而不是powershell.
Get-AppXProvisionedPackage -online | findstr ^DisplayName
Get-AppXProvisionedPackage -online | Select-String -pattern "DisplayName"
Run Code Online (Sandbox Code Playgroud)
powershell新手:)
我遇到了从文件中提取文本的问题。这是我最终想要做的:
这是我正在处理的部分的片段:
$FileContents = Select-String -Path "C:\Users\me\Documents\file.ini" -Pattern "NoRequest"
Run Code Online (Sandbox Code Playgroud)
当我使用该行时,我得到以下信息:
PS> $文件内容 Documents\file.ini:222:NoRequest = false Documents\file.ini:281:NoRequest = false Documents\file.ini:347:NoRequest = false Documents\file.ini:387:NoRequest = false Documents\file.ini:1035:NOREQUEST = true Documents\file.ini:1047:NoRequest = true Documents\file.ini:1160:NOREQUEST = true Documents\file.ini:1242:NOREQUEST = true
这就是我想要得到的:
PS> $文件内容 错误的 错误的 错误的 错误的 真的 真的 真的 真的
我尝试将代码通过管道传输到Select-Object -Last 1解决方案中,但由于我的变量是一个数组,因此它选择数组中的最后一行,而不是每行的最后一个字。我还想过只取数组并执行“数组中的 foreach 行,将除最后一个字外的所有内容都剪掉”。我猜这会奏效,但似乎这是一个笨拙的解决方案。我确信有一个简单有效的解决方案,但我只是没有看到。
我想查看我是否正在运行特定的 wsl 发行版(Windows 10 Home、WSL 2):
PS C:\Users\User> wsl --list --running
Windows Subsystem for Linux Distributions:
Ubuntu (Default)
MyDistro
PS C:\Users\User> wsl --list --running | Select-String -Pattern "MyDistro"
PS C:\Users\User>
Run Code Online (Sandbox Code Playgroud)
无输出。我以前Get-Member看到输出是一个字符串;如果我通过类似的事情来运行它,| Out-String -stream那没有什么区别。
我可以Select-String .用or进行匹配Select-String .*,但它匹配所有内容,这没有帮助。
是的,我想看看是否有一个具有特定名称的正在运行的发行版。在 PowerShell 中是否有更好的方法来做到这一点?
当我跑:
$SearchStr = "c:\programFiles\xyz"
Select-String -pattern $SearchStr -path $env:SystemRoot\win.ini
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息,指出该字符串"不是有效的正则表达式"
相比之下,它$SearchStr不包含特殊字符时工作正常.
为什么它不适用于特殊字符,如\?