如何修改Select-String输出格式:将行号置于路径括号内
修改Select-String输出格式的方法
原输出格式:
\\path-to-logfile\copy2xl.log:390:NOTE: At least one W.D format was too small for the number to be printed. The decimal may be shifted by the "BEST" format.
目标输出格式:
\\path-to-logfile\copy2xl.log(390):NOTE: At least one W.D format was too small for the number to be printed. The decimal may be shifted by the "BEST" format.
可以通过PowerShell管道结合ForEach-Object重构输出内容,具体命令如下:
# 替换为你的目标文件路径和匹配模式 Select-String -Path "\\path-to-logfile\copy2xl.log" -Pattern "W.D format was too small" | ForEach-Object { # 按目标格式拼接路径、行号和行内容 "$($_.Path)($($_.LineNumber)):$($_.Line)" }
核心逻辑:
Select-String返回的匹配对象自带Path(文件路径)、LineNumber(行号)、Line(匹配行内容)属性- 通过字符串插值将这三个属性按
路径(行号):行内容的格式重新组合,即可得到目标输出
内容的提问来源于stack exchange,提问作者data null
相关产品推荐
相关产品推荐

