当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 选择字符串忽略大小写

作者:精品下载站 日期:2024-12-14 17:02:07 浏览:13 分类:玩电脑

PowerShell 选择字符串忽略大小写


在 PowerShell 中,Select-String 默认忽略大小写。它不区分大小写。它使用 Pattern 参数来匹配物种字符串或正则表达式模式并返回与模式匹配的结果。

Select-String 忽略文本的大小写,如果模式是搜索单词“ShellGeek”,它将匹配“ShellGeek”、“shellgeek”或“shellGeek”等...无论字母是否存在,它都会匹配文本是小写或大写。

在本文中,我们将讨论如何使用 Select-String 命令通过使用 -CaseSensitive 参数来演示 Select-String 忽略大小写和 CaseSensitive 搜索。

PowerShell 选择字符串忽略大小写

使用 PowerShell 中的 Select-String 来匹配使用指定模式字符串或正则表达式模式的文本。此命令默认情况下忽略大小写并返回结果,无论字符串中的字母是大写还是小写。

# Get the contents of the file
Get-Content -Path D:\Logs\Log1.txt

# Use the Select-String to search for text
Get-Content -Path D:\Logs\Log1.txt | Select-String -Pattern "shellgeek"  

在上面的 PowerShell 脚本中,Get-Content cmdlet 获取文件内容并将其传递给 Select-String cmdlet 以搜索文本“shellgeek”

Select-String cmdlet 搜索文本时不考虑大小写,它会忽略大小写并返回匹配的文本。

上述 PowerShell 脚本的输出是:

[玩转系统] PowerShell 选择字符串忽略大小写

PowerShell 选择字符串区分大小写的搜索

PowerShell Select-String 默认情况下会忽略大小写,但是,要执行区分大小写的搜索,请使用其 CaseSensitive 参数。

# Get the content of the file
Get-Content -Path D:\Logs\Log1.txt

# Use Select-String -CaseSensitive to search a text
Get-Content -Path D:\Logs\Log1.txt | Select-String -Pattern "shellgeek" -CaseSensitive

在上面的 PowerShell 脚本中,Select-String 使用 CaseSensitive 参数搜索指定字符串并返回与该字符串完全匹配的结果。

上述 PowerShell 脚本的输出是:

PS C:\> Get-Content -Path D:\Logs\Log1.txt                                                                                  ShellGeek Tutorials
shellgeek tutorials
Shellgeek Tutorials
shellgeek Tutorials

PS C:\> Get-Content -Path D:\Logs\Log1.txt | Select-String -Pattern "shellgeek" -CaseSensitive                              
shellgeek tutorials
shellgeek Tutorials


PS C:\>   

结论

希望上面关于如何在文件中使用PowerShell Select-String Ignore case和CaseSensitive搜索的文章对您有所帮助。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯