分裂,重新安排,不包括PowerShell中文本文件的字符串以输出到另一个文本文件



我使用piconfig从osisoft pi系统中输出了一个标签的名称,时间和值。我不能对piconfig脚本中的内容进行任何形式编辑,因此我试图将文本文件(每日阶段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 buildingname0

l01_b000_buildingname0_citect_more_tag_info_here,22-feb-17 14:56,3.808521E 07

B111 buildingName1

l01_b111_buildingname1_mainelectric_111a01me_alc,23-apr-15 08:45,64075。

B333 BuildingName3

l01_b333_buildingname3_subelectric_333b03se_mozart,2-dec-16 18:45,70371。 l01_b333_buildingname3_citect_more_tag_333_info_here,4-Jan-17 10:08,111730。

我基本上是一个新手(昨天的生成和通过电子邮件发送报告的活动对我来说是一项重要壮举),因此我正在尝试解决人们以前提出的基本文章和问题。我设法使用本文添加了标题:https://www.petri.com/powershell-import-cmdlet-cmdlet-parse-comma-delimited-comma-delimited-cmsv-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解析文本文件,但我陷入困境,因为这个示例并不完全适用。

根据我阅读的内容,《变化》在这里无法真正起作用,因为我有一条以上的信息。如果有人可以将我指向良好的方向(或者是否可以按照我上面的示例进行操作),我将非常感谢。

此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.

您所拥有的是CSV格式化数据,因此可以使用Import-CSV,它是使用CSV数据的合适工具... 但是想要输出不是CSV数据。这是一些带有标题和空白的随机报告,因此使用CSV工具并逐行处理文件并不是很简单的,因为您将在标题之间进行分组时遇到麻烦。

从我阅读的内容中,《变化》在这里无法真正起作用,因为我有一条以上的信息。

由于您不将行内容视为不同的信息,并且日期和数字不会包含"手动"或" 他的'",这是可行的。

# 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

最新更新