【问题标题】:Splitting, rearranging, excluding strings from a text file in Powershell to output to another text file拆分、重新排列、排除 Powershell 中文本文件中的字符串以输出到另一个文本文件
【发布时间】:2017-04-15 05:13:19
【问题描述】:

我正在使用 piconfig 从 OSIsoft PI System 输出一份陈旧的标签报告,其中包含标签的名称、时间和值。我无法对 piconfig 脚本中的显示方式进行任何编辑,因此我尝试将文本文件 (daily-stale-tags-report.txt) 解析为新的文本文件,该文件将自动通过电子邮件发送每天早上出去。文本文件目前看起来像这样:

L01_B000_BuildingName0_Citect_more_tag_info_here,22-Feb-17 14:56:23.55301,3.808521E+07
L01_B111_BuildingName1_MainElectric_111A01ME_ALC,23-Apr-15 08:45:00,64075.
L01_B111_BuildingName1_MainSteam_111TC1MS_His_Mozart,20-Jan-17 22:21:34,88778.
L01_B333_BuildingName3_MainWater_333E02MW_Manual,1-Dec-16 18:00:00,4.380384E+07
L01_B333_BuildingName3_SubElectric_333B03SE_Mozart,2-Dec-16 18:45:00,70371.
L01_B333_BuildingName3_Citect_more_tag_333_info_here,4-Jan-17 10:08:33.24501,111730

我需要排除任何以“_Manual”结尾或包含“_His_”的标签,目的是输出一个看起来更像这样的文本文件:

B000 建筑物名称0

L01_B000_BuildingName0_Citect_more_tag_info_here,22-Feb-17 14:56,3.808521E+07

B111 BuildingName1

L01_B111_BuildingName1_MainElectric_111A01ME_ALC,2015 年 4 月 23 日 08:45,64075。

B333 BuildingName3

L01_B333_BuildingName3_SubElectric_333B03SE_M​​ozart,2016 年 12 月 2 日 18:45,70371。 L01_B333_BuildingName3_Citect_more_tag_333_info_here,2017 年 1 月 4 日 10:08,111730。

在这方面我基本上是个新手(昨天生成并成功通过电子邮件发送报告的活动对我来说是一项重大成就),所以我正在努力解决人们之前提出的基本文章和问题。我设法使用这篇文章添加了标题:https://www.petri.com/powershell-import-csv-cmdlet-parse-comma-delimited-csv-text-file,我认为它看起来像这样:

$input = import-csv "c:daily-stale-tags-report.txt" -header Tag,Date,Value

然后我继续阅读这篇文章https://www.petri.com/powershell-string-parsing-with-substring,尝试使用下划线作为分隔符拆分我的数据,目的是提取建筑代码(例如 B000)和建筑名称。

ForEach ($tag in $input) {$t = ($s -split '_',4)[1..2]}

最后,我尝试使用这篇文章 powershell Parsing a text file,但我被困住了,因为这个例子并不完全适用。

根据我的阅读,Get-Content 在这里不会真正起作用,因为我在一行中有多个信息。如果有人可以为我指明一个好的方向(或者这是否可以按照我上面的示例的方式完成),我将不胜感激。

【问题讨论】:

  • 在 ForEach 中你没有使用 $tag。

标签: powershell parsing split text-files


【解决方案1】:

此 PowerShell 脚本接近所需的输出:

$File = "daily-stale-tags-report.txt" 
import-csv $File -header Tag,Date,Value|
  Where {$_.Tag -notmatch '(_His_|_Manual$)'}|
    Select-Object *,@{Name='Building';Expression={"{0} {1}" -f $($_.Tag -split '_')[1..2]}}|
      Format-table -Groupby Building -Property Tag,Date,Value

输出:

   Building: B000 BuildingName0

Tag                                              Date                     Value
---                                              ----                     -----
L01_B000_BuildingName0_Citect_more_tag_info_here 22-Feb-17 14:56:23.55301 3.808521E+07


   Building: B111 BuildingName1

Tag                                              Date               Value
---                                              ----               -----
L01_B111_BuildingName1_MainElectric_111A01ME_ALC 23-Apr-15 08:45:00 64075.


   Building: B333 BuildingName3

Tag                                                  Date                    Value
---                                                  ----                    -----
L01_B333_BuildingName3_SubElectric_333B03SE_Mozart   2-Dec-16 18:45:00       70371.
L01_B333_BuildingName3_Citect_more_tag_333_info_here 4-Jan-17 10:08:33.24501 111730.

【讨论】:

    【解决方案2】:

    您所拥有的是 CSV 格式的数据,所以找到 Import-CSV 是个不错的选择,它是处理 CSV 数据的正确工具...但是您想要的输出不是 CSV数据。这是一些带有标题和空白行的随机报告,因此使用 CSV 工具并不是很简单,并且逐行处理文件也无济于事,因为您将无法将标题之间的内容分组。

    根据我的阅读,Get-Content 在这里不会真正起作用,因为我在一行中有多个信息。

    由于您没有将行内容视为不同的信息,并且日期和编号不会包含“手册”或“他的”,因此它是可行的。

    # Get the lines of the file, drop any that have _his_ or manual, in them
    # ('manual,' is a cheeky assumption that the word is at the end of the tag)
    Get-Content report.txt | Where-Object { $_ -notmatch '_his_|manual,' } |
    
        # Split the line by _ and take things 1 and 2 for the building name section header.
        # Group the lines by calculated building name.
        Group-Object -Property { $parts = $_ -split '_'; "$($parts[1]) $($parts[2])" } |
    
        # process the groups, outputting the building name then all the lines 
        # relating to it, then a blank line
        ForEach-Object { 
            $_.Name
            $_.Group
            ""
        }
    
    e.g.
    
    B000 BuildingName0
    L01_B000_BuildingName0_Citect_more_tag_info_here,22-Feb-17 14:56:23.55301,3.808521E+07
    
    B111 BuildingName1
    L01_B111_BuildingName1_MainElectric_111A01ME_ALC,23-Apr-15 08:45:00,64075
    
    B333 BuildingName3
    L01_B333_BuildingName3_SubElectric_333B03SE_Mozart,2-Dec-16 18:45:00,70371
    L01_B333_BuildingName3_Citect_more_tag_333_info_here,4-Jan-17 10:08:33.24501,111730
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多