Windows 里的 grep:Select-String
用正则搜索文本文件和日志内容,返回文件名、行号和匹配行。
运行 `Select-String -Path "*.log" -Pattern "error"`,会打印每条匹配行及其文件名和行号。
01命令
命令
$ Select-String -Path "$env:USERPROFILE\Documents\*.log" -Pattern "error"02作用说明
Select-String 是 PowerShell 版的 grep:-Path 支持通配符,-Pattern 默认按正则解析(匹配字面文本时加 -SimpleMatch),输出对象携带 Filename、LineNumber 和 Line。也可以通过管道配合 Get-ChildItem -Recurse 搜索整个目录树:`gci -Recurse *.txt | Select-String pattern`。注意默认匹配不区分大小写,需要时加 -CaseSensitive。
03风险
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- Select-String (Microsoft.PowerShell.Utility)依据: Documents Select-String with -Path, -Pattern (regex), -CaseSensitive and -SimpleMatch parameters for searching file content.
最后验证: 2026-09-17 · 分类: 文件与文件夹