powershell to get one specific colume value from one row

Yonglong YL44 Wang 61 Reputation points
2021-10-21T02:41:40.113+00:00

if the output is like this

java 14.59%

i want to get the the second colume (14.59% ) and save in one parameter.
how to do that ?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,354 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 44,621 Reputation points
    2021-10-21T19:56:11.047+00:00

    This works (assuming the column separator's a space):

    $string = "java 14.59%"
    ($string -split " ")[1]
    

    If there are multiple spaces (or tabs or other 'whitespace' characters) separating the columns, then this will work:

    $string -replace  "^.+?\s+(.+)$",'$1'
    

    Or even this (if it's always spaces as separators):

    $string.split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)[1]
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Yonglong YL44 Wang 61 Reputation points
    2021-10-25T07:48:51.73+00:00
     $string -replace  "^.+?\s+(.+)$",'$1'   is working for me  , thanks you very much.
    

    now , i can get 14.59%. another question is how to remove % form 14.59% ?
    what i want is 14.59 .


  2. Andreas Baumgarten 95,411 Reputation points MVP
    2021-10-25T16:54:11.26+00:00

    Hi @Yonglong YL44 Wang ,

    this should help:

    "14.59%" -Replace "%",""  
      
    # or this way:  
      
    $b = "14.59%"  
    $b.Replace("%","")  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten