在 Powershell 中从文本文件中拆分、重新排列、排除字符串以输出到另一个文本文件 [英] Splitting, rearranging, excluding strings from a text file in Powershell to output to another text file

查看:39
本文介绍了在 Powershell 中从文本文件中拆分、重新排列、排除字符串以输出到另一个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am outputting a stale tag report from the OSIsoft PI System using piconfig, with the name, time, and value of the tag. I cannot do any sort of editing to how this is presented from within the piconfig script, so I am instead trying to parse the text file (daily-stale-tags-report.txt) to a new text file, which will be automatically emailed out each morning. The text file currently looks something like this:

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_"的标签,目的是输出一个看起来更像这样的文本文件:

I need to exclude any tag ending in '_Manual' or that contains '_His_', with the goal of outputting a text file that appears more like this:

B000 BuildingName0

B000 BuildingName0

L01_B000_BuildingName0_Citect_more_tag_info_here,22-Feb-1714:56,3.808521E+07

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

B111 BuildingName1

B111 BuildingName1

L01_B111_BuildingName1_MainElectric_111A01ME_ALC,23-Apr-1508:45,64075.

L01_B111_BuildingName1_MainElectric_111A01ME_ALC,23-Apr-15 08:45,64075.

B333 BuildingName3

B333 BuildingName3

L01_B333_BuildingName3_SubElectric_333B03SE_M​​ozart,16 年 12 月 2 日18:45,70371.L01_B333_BuildingName3_Citect_more_tag_333_info_here,4-Jan-1710:08,111730.

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-csv-cmdlet-parse-comma-delimited-csv-text-file 我认为它看起来像这样:

I am basically a newbie at all this (yesterday's activity of generating and successfully emailing the report was a major feat for me), so I am trying to work off of basic articles and questions people have previously asked. I managed to add headers using this article: https://www.petri.com/powershell-import-csv-cmdlet-parse-comma-delimited-csv-text-file which I assume would look something like this:

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

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

I then moved on to this article https://www.petri.com/powershell-string-parsing-with-substring to try and split up my data using the underscore as the delimiter, with the goal of extracting the Building Code (e.g. B000) and BuildingName.

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

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

Lastly, I was trying to use this article powershell Parsing a text file but I'm stuck as this example doesn't quite apply.

根据我所读到的内容,Get-Content 在这里不会真正起作用,因为我在一行中有不止一条信息.如果有人能给我指出一个好的方向(或者这是否可以按照我上面的例子来完成),我将不胜感激.

From what I've read, Get-Content wouldn't really work here because I have more than one piece of information on a line. If anyone could point me in a good direction to head (or whether this can even be done the way I have my example above), I'd greatly appreciate it.

推荐答案

这个 PowerShell 脚本接近所需的输出:

This PowerShell script comes near desired output:

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

这篇关于在 Powershell 中从文本文件中拆分、重新排列、排除字符串以输出到另一个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆