Tuesday, 15 May 2012

powershell - Split an item on the pipeline -



powershell - Split an item on the pipeline -

i have text file contain similar:

code,server,success,failures abc,abcsrv01,4,5

depending on logic in script - success or failure count updated. @ moment easiest way can think of doing to:

$currentstats = get-content "c:\onl\stats.txt" | where-object {$_ -like "abc,abcsrv01,*"} $split = $currentstats.split(",") $newsuccesscount = $split[2] -as [int] $newsuccesscount++ $newstats = "abc,abcsrv01," + $newsuccesscount + "," + $split[3] $updatedstats1 = get-content "c:\onl\stats.txt" | where-object {$_ -notmatch "abc,abcsrv01,*"} $newstatstxt = $updatedstats1 + $newstats $newstatstxt | out-file "c:\onl\stats.txt"

however, can't help sense there must easier way of achieving - maybe on pipeline?

interacting file object beneficial , code.

$currentstats = import-csv "c:\onl\stats.txt"

then can access properties success , failures object properties. not sure doing info this.

$currentstats = import-csv "e:\temp\data.txt" $update = $currentstats | foreach-object{ if ($_.code -eq "abc"){ $_.success = [int]$_.success + 1 } else { $_.failures = [int]$_.failures - 1 } $_ } $updatedstats | export-csv -path "c:\onl\stats.txt"

that is, obviously, not going should illustrate potential have.

powershell

No comments:

Post a Comment