Split & token delimiter in string
i have data in txt file want break tokens. data looks this
definitionid: "atlanta"
definitiontype: “location”
root: “\\atlfs\”
jobs: “atlanta jobs”
flexlm_servers: “@atllic01;@dallic01;@tamlic01”
rs_accel: "atlrevitsvr01"
and want different things depending on value of first of each token pair. however, run problems when have line like...
root: “r:\”
because token delimiter included in string of second token bad split. 1 option use different token delimiter. or since name/value pair switch different format {name}"value" or like, variety of historical reasons rather not. and, being new powershell have hope perhaps there easy way this, filtering out in quotes prior evaluating token delimiter. or flag use naked token delimiters. thoughts?
also, know "right" answer data standpoint use xml, again historical (and sociological) reasons prefer not to, if can make simple txt file work. day go xml, in conjunction gui data, don't have time first version.
any suggestions appreciated
hi!
i have suggested xml-stuff etc hadn't said didn't wan't go down road.. =)
anyway, way split each line using split() function. functiona accepts 2 arguments, first delimeter, , second number of array positions return maximum. instance:
$result = "word1 word2 word3 , rest of string".split(" ",2)
would result in
ps> $result[0] word1 ps> $result[1] word2 word3 , rest of string
when use get-content read content file can send straigh foreach so:
$file = get-content .\config.txt foreach($row in $file) { $rowarray = $row.split(" ",2) $key = $rowarray[0].replace(":") $value = $rowarray[1] ...... }hope helps!
Windows Server > Windows PowerShell
Comments
Post a Comment