Extracting a portion of a string then using it to match with other strings in Powershell -
i asked assistance parsing text file , have been using code script:
import-csv $file -header tag,date,value| {$_.tag -notmatch '(_his_|_manual$)'}| select-object *,@{name='building';expression={"{0} {1}" -f $($_.tag -split '_')[1..2]}}| format-table -groupby building -property tag,date,value
i've realized since that, while code filters out tags containing _his
or _manual
, need filter tags associated _manual
. example, following tags present in text file:
l01_b111_buildingname1_mainelectric_111a01me_alc,13-apr-17 08:45,64075 l01_b111_buildingname1_mainelectric_111a01me_cleansed,13-apr-17 08:45,64075 l01_b111_buildingname1_mainelectric_111a01me_consumption,13-apr-17 08:45,10.4 l01_b333_buildingname3_mainwater_333e02mw_manual,1-dec-16 18:00:00,4.380384e+07 l01_b333_buildingname3_mainwater_333e02mw_cleansed,1-dec-16 18:00:00,4.380384e+07 l01_b333_buildingname3_mainwater_333e02mw_consumption,1-dec-16 18:00:00,25.36
the 333e02mw_manual
string excluded using current code, how exclude 333e02mw_cleansed
, 333e02mw_consumption
? feel need allow me extract 8-digit code before each _manual
instance , use find other strings {matchingcode}
xxx_xxxx_xxxxxxxxxxx_xxxxxxxxxx_matchingcode_cleansed xxx_xxxx_xxxxxxxxxxx_xxxxxxxxxx_matchingcode_consumption
i know there -like
-contains
, -match
operators , i've seen these posts on using substrings , regex, how extract matchingcode have match to? this post seems come closest goal, i'm not sure how apply powershell.
you can find every tag ends _manual
, create regex pattern matches of parts before _manual
. ex.
$data = import-csv -path $file -header tag,date,value #create regex matches prefixes has manual row (matches using value before _manual) $excludemanualpattern = ($data | foreach-object { if($_.tag -match '^(.*?)_manual$') { [regex]::escape($matches[1]) } }) -join '|' $data | where-object { $_.tag -notmatch '_his_' -and $_.tag -notmatch $excludemanualpattern } | select-object -property *,@{name='building';expression={"{0} {1}" -f $($_.tag -split '_')[1..2]}}| format-table -groupby building -property tag,date,value
Comments
Post a Comment