2017-02-24 52 views
0

我使用piconfig從標籤的名稱,時間和值中輸出OSIsoft PI System的陳舊標籤報告。我無法對piconfig腳本中的內容進行任何編輯,因此我試圖將文本文件(daily-stale-tags-report.txt)解析爲新的文本文件,該文件將自動通過電子郵件發送每天早上出去。該文本文件目前看起來是這樣的:分割,重新排列,從Powershell中的文本文件中排除字符串以輸出到另一個文本文件

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_M​​ozart,2日 - 12月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我以爲會是這個樣子:

$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在這裏不會真正起作用,因爲我在一行上有不止一條信息。如果任何人都可以指出我朝着一個好的方向前進(或者這是否可以像我上面的例子那樣完成),我將不勝感激。

+0

在ForEach中,您沒有使用$標籤。 –

回答

2

這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 
相關問題