【问题标题】:Parse info from Text File - Powershell从文本文件解析信息 - Powershell
【发布时间】:2022-11-18 00:35:48
【问题描述】:

这里是初学者,我正在处理错误日志文件和库,当前我正在进行的步骤是从 txt 文件中提取特定信息。

我目前拥有的代码是...

$StatusErr = "Type 1","Type 2"

for ($i=0; $i -lt $StatusErr.length; $i++) {
  get-content C:\blah\Logs\StatusErrors.TXT | 
    select-string $StatusErr[$i] |
    add-content C:\blah\Logs\StatusErrorsresult.txt
}

当它工作时,我需要它显示为

Type-1-Description  
2-Description  
Type-1-Description  
2-Description  
Type-1-Description  
2-Description

等等

它目前显示为

Type 1 = Type-1-Description  
Type 1 = Type-1-Description  
Type 1 = Type-1-Description  
Type 2 = 2-Description  
Type 2 = 2-Description  
Type 2 = 2-Description  

我不确定如何更改排列并删除不需要的空格和 = 号

【问题讨论】:

  • edit你的问题提供有代表性的样本输入,产生你已经展示的样本输出。
  • 希望这会有所帮助,即使发布问题也很困难,我仍然无法正确显示缩进。当前在 Type 2 和 = 符号之间有 10 个空格。

标签: powershell parsing


【解决方案1】:
  • 您需要搜索两个都中的模式单身的Select-String 调用以按顺序获取匹配行。

  • 您需要使用 regex 模式才能仅捕获和输出部分匹配的行。

$StatusErrRegexes = '(?<=Type 1s+=s+)[^ ]+', '(?<=Type 2s+=s+)[^ ]+'

get-content C:lahLogsStatusErrors.TXT | 
  select-string $StatusErrRegexes |
  foreach-object { $_.Matches.Value } |
  add-content C:lahLogsStatusErrorsresult.txt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 2019-03-22
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多