none
key and value separation from input string RRS feed

  • Question

  • I try to takeout key and value pair from below string but not getting success, string.split is too tedious and no comma seperate but a space seperate string is as input, how to format out put as key and value

    $str=@"
    this is some info
    author:mine
    key:noodle
    UAT:someval
    some type of useless info
    some type of useless info
    some type of useless info
    production: ddlg
    SIT:ppr
    QA:testdd
    Test:makemy
    VIT:teriff
    "@

    I want to seperate key:values in string as

    key is SIT and value ppr

    key is QA and value testdd

    key is Test and value is makemy

    I have used hashtable=@{} and string.split(how to separate by space) but because line "some type of useless info" and uneven space at the end not getting success


    • Edited by himanshu_kulkarni Tuesday, February 13, 2018 1:27 PM formatting and spell check
    Tuesday, February 13, 2018 1:20 PM

Answers

  • Best guess:

    $str = @'
    this is some info
    author:mine
    key:noodle
    UAT:someval
    some type of useless info
    some type of useless info
    some type of useless info
    production: ddlg
    SIT:ppr
    QA:testdd
    Test:makemy
    VIT:teriff
    configkey:ID
    '@ -split "`n"
    
    $hash = @{ }
    $str | ?{$_ -match ':'} | %{
        $hash.Add(
            (($_ -split ':')[0]),
            (($_ -split ':')[1])
        )}
    #display results
    $hash


    \_(ツ)_/

    Thursday, February 15, 2018 12:35 AM

All replies

  • Test each line for a ":" and split the line.  THat's all.

    \_(ツ)_/

    Tuesday, February 13, 2018 1:41 PM
  • that gives me all the : separate words one after the other

    I want output as $key=VDI and $val=teriff , key value pair format 

     
    Wednesday, February 14, 2018 1:07 PM
  • The output you say you want is not clear from the input data.

    Without an exactly precise description of the data transformation you want to perform, it will be a frustrating game of back-and-forth where we say "what about this?" and you say "no, that's not it." Over and over.

    You must produce a precise and exact description of what you want to do with the input data. It would also help to know why you think you need to use a hashtable.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, February 14, 2018 4:46 PM
    Moderator
  • 1.I am getting input string as $str
    2.from this string I want to take out configkey="ID" as main key and value for receptive environment as
    3.<conf key="ID" value="ppr" /> for SIT environment this node I will add into SIT1.xml, SIT2.xml files
    4.for UAT1.xml, UAT2.xml key value pair is <conf key="ID" value="someval" />
    5.for VIT1.xml,VIT2.xml key value pair is <conf key="ID" value="teriff" />
    6.also I want to skip the lines like "some type of useless info" from $str
    $str=@"
    this is some info
    author:mine
    key:noodle
    UAT:someval
    some type of useless info
    some type of useless info
    some type of useless info
    production: ddlg
    SIT:ppr
    QA:testdd
    Test:makemy
    VIT:teriff
    configkey:ID
    "@


    Wednesday, February 14, 2018 6:17 PM
  • We may have a language barrier, but unfortunately that's of no help at all.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, February 14, 2018 6:24 PM
    Moderator
  • Best guess:

    $str = @'
    this is some info
    author:mine
    key:noodle
    UAT:someval
    some type of useless info
    some type of useless info
    some type of useless info
    production: ddlg
    SIT:ppr
    QA:testdd
    Test:makemy
    VIT:teriff
    configkey:ID
    '@ -split "`n"
    
    $hash = @{ }
    $str | ?{$_ -match ':'} | %{
        $hash.Add(
            (($_ -split ':')[0]),
            (($_ -split ':')[1])
        )}
    #display results
    $hash


    \_(ツ)_/

    Thursday, February 15, 2018 12:35 AM