首页 > 解决方案 > 在powershell windows 8中搜索具有双引号的字符串

问题描述

我有一个包含以下内容的文件 -

serial_no= 1 name="abc.txt" type="Text" date="25-06-18"
serial_no= 2 name="abc.bmp" type="Bitmap" date="23-06-18"
serial_no= 3 name="abc.jpg" type="Image" date="21-06-18"
serial_no= 4 name="abc2.txt" type="Text" date="26-06-18"
serial_no= 5 name="abc.log" type="LogFile" date="19-06-18"
serial_no= 6 name="abc.exe" type="Program" date="20-06-18"
serial_no= 7 name="abc.jar" type="Java Archive" date="25-06-18"
serial_no= 8 name="abc3.txt" type="Text" date="11-06-18"

我想搜索type="Text"

我尝试了 Select-String 与 -pattern 选项的许多值,但总是得到错误 -

命令 -

Select-String -path "path\filename.log" -pattern "type="Text""

错误 -

Select-String : A positional parameter cannot be found that accepts argument 'Text'.
At line:1 char:1
+ Select-String -path "...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Select-String], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SelectStringCommand

有人能告诉我如何修改我的命令来搜索这样一个带有双引号的字符串吗?

标签: windowspowershellselect-string

解决方案


'您可以通过使用而不是"在外部找到字符串:

Select-String -path "path\filename.log" -pattern 'type="Text"'

为您提供正确的输出(例如):

filename.log:1:serial_no= 1 name="abc.txt" type="Text" date="25-06-18"
filename.log:4:serial_no= 4 name="abc2.txt" type="Text" date="26-06-18"
filename.log:8:serial_no= 8 name="abc3.txt" type="Text" date="11-06-18"

推荐阅读