powershell - 消除日志行中的重复字段

标签 powershell

我有一些日志行,其中的信息字段重复出现,第一次用逗号和空格隔开,第二次用分号隔开,我想摆脱它们的第二次出现,即单词(SECOND)不在日志中,我将其放在此处以使其更清晰

targets:somehost state:Memory\Buffers=398672, Memory\Cached=4620216, Memory\MemFree=833748, Memory\MemTotal=8001352 (SECOND) Memory\Buffers=398672;Memory\Cached=4620216;Memory\MemFree=833748;Memory\MemTotal=8001352  type:Unix Resources

我在考虑使用替换。
 %{$_ -replace "Memory\\Buffers=([0-9]+);Memory\\Cached=([0-9]+);Memory\\MemFree=([0-9]+);Memory\\MemTotal=([0-9]+)",""}

但是日志中有很多字段,为了使可读性更好,我在这里没有输入。
有一个更好的方法吗?

最佳答案

您可以删除第二个出现的Memory\Buffers=(带有正look-behind assertion ( (?<=...) )的开始(包括):

$string -replace '(?<=Memory\\Buffers=.*?)\s*Memory\\Buffers.*$'

如您所见,使用正look-ahead assertion ( (?=...) ),您可以指定第二个序列的停止位置:
$string -replace '(?<=Memory\\Buffers=.*?)\s*Memory\\Buffers.*(?=\stype:)'

关于powershell - 消除日志行中的重复字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60366058/

相关文章:

powershell - PowerShell无法识别基本命令?术语 'get'无法识别为cmdlet的名称(错误)

shell - 进行键绑定(bind)以运行上一个或最后一个 shell 命令

python-3.x - Discord.py 在 Windows 10 上安装失败(Python 3.9,包括日志)

powershell - VirtualBox + Powershell?

PowerShell - 过滤唯一值

powershell - 如何使用 7zip powershell 包含文件和文件夹

.net - 使用 PowerShell 连接到 Azure FTP 但在 C# 中工作时出现错误 501

windows - 如何在 Powershell 中使用 New-Item cmdlet 将 SSH 私钥写入文件?

Azure Functions PowerShell - 从 Azure 存储表访问时间戳值

arrays - 如何在 PowerShell 中将 HashSet 转换为 ArrayList?