简体   繁体   English

包含$的文件中的Powershell搜索字符串

[英]Powershell Search String in File Containing $

I want to search the following string in a text file: $$u$$ 我想在文本文件中搜索以下字符串: $$u$$

I tried select-string , get-content , .Contains . 我尝试了select-stringget-content.Contains It seems to me it's not possible. 在我看来这是不可能的。

I used this for the search as a variable: $ToSearch = "'$'$u'$'$" 我使用它作为变量进行搜索: $ToSearch = "'$'$u'$'$"

It always gives false result. 它总是给出错误的结果。

It is because most search filters are relying on regex. 这是因为大多数搜索过滤器都依赖于正则表达式。 The $ symbol in regex needs to be escaped 正则表达式中的$符号需要转义

Without knowing more of what you're trying to accomplish I can't give much of an example, but here is one: 在不了解更多您要完成的工作的情况下,我无法举太多例子,但这是一个例子:

'this is a $$u$$ test' -replace "\$",""

The '\\' is what is escaping the character - meaning to translate it literally. '\\'是转义字符的含义-意味着按字面意义进行翻译。

Edit: Per comment 编辑:每个评论

$Val = 'this is a $$u$$ test'
$Val | Select-String "\$+\w\$+" -quiet

-Quiet switch returns t/f rather than a string value. -安静的开关返回t / f而不是字符串值。

The $ is a reserved character in regex, which is complicating your search. $是正则表达式中的保留字符,这会使您的搜索复杂化。

The default search pattern type for Select-String is regex, but you can also specify -Simplematch or -Wildcard , either of which will eliminate the need to escape the $. Select-String的默认搜索模式类型是regex,但是您也可以指定-Simplematch-Wildcard ,这两种类型都将消除对$进行转义的需要。 If you use -Wildcard, you'll need to include the wilcard * at either end of the match - '*$$u$$*' . 如果使用-Wildcard,则需要在比赛的任一行末添加通配符*- '*$$u$$*' For simplematch, just use the string you want to match for - '$$u$$' . 对于simplematch,只需使用要匹配的字符串- '$$u$$'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 powershell写入包含字符串的文件的名称 - powershell write name of file containing string 使用 PowerShell 搜索包含两次或多次出现的特定字符串的文件名 - Search for filenames containing two or more occurrence of a specific string using PowerShell 在powershell中使用搜索字符串编辑文件 - Editing a file using search string in powershell 在Linux中使用带有字符串的文本文件搜索包含该字符串的文件 - Using a text file with string to search for files containing that string in Linux 如何在文件中搜索字符串并打印包含该字符串的行? - How to search for a string in a file and print the line containing that string? 具有给定名称的Linux搜索文件以递归方式包含字符串 - Linux search file with given name containing string recursively 使用 Powershell 操作 txt 文件中的字符串 - 在包含“.”的每一行中搜索单词。 并将它们保存在新的txt文件中 - Manipulate strings in a txt file with Powershell - Search for words in each line containing "." and save them in new txt file 在Powershell中搜索颜色字符串 - Search for color string in powershell 搜索字符串并删除其他所有内容并使用 Powershell 粘贴到新文件 - Search string and delete everythig else and paste to new file with Powershell 创建一个新的包含定义的子字符串的PowerShell字符串 - Create a new PowerShell string containing defined substrings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM