Powershell string parsing


  1. Posts : 11
    Windows 10, Debian 9
       #1

    Powershell string parsing


    Lets say I have a string with several columns separated by spaces. The number of spaces and the data in each column are unknown. How would you grab what's in col3 using PowerShell? I know how I would do it with Awk, but that won't necessarily be available to me.

    col1 col2 col3 col4
      My Computer


  2. Posts : 2,068
    Windows 10 Pro
       #2
      My Computers


  3. Posts : 11
    Windows 10, Debian 9
    Thread Starter
       #3

    The only issue with split is that I don't know how many spaces are between the columns. For instance, if I want what is in the third column, this example would not work.
    Code:
    $a = "ABC    69278  DEF      10278"
    $a.Split()[2]
    I must Split twice
    Code:
    $a = "ABC    69278  DEF      10278"
    $a = $a.Split() -ne""
    $a.Split()[2]
    I feel like I'm overcomplicating things and this could be simplified somehow. Is there a better way?
    Last edited by jnich; 02 May 2017 at 22:29.
      My Computer


  4. Posts : 93
    Windows 10
       #4

    Hi Jnich,

    There are two ways you should approach this string splitting: by using the second parameter of the .Split() method, or by using the -split operator.

    The .Split() .NET method of the string type has six overloads, one of which allows you to specify [System.StringSplitOptions]::RemoveEmptyEntries after the string you wish to split by.
    Code:
    $a.Split(' ', [System.StringSplitOptions]::RemoveEmptyEntries)

    The second way, which is shorter and more PowerShell-ic, is to use the -split operator. Itís typical use involves two arguments on either side of the operator, e.g., $a -split ' ', but if you do this youíll find itís equivalent to doing $a.Split(' ') which doesnít help. Instead thereís a second overload that lets you specify a single argument, to the right of the operator, and this will split the string by whitespace and automatically remove empty entries: -split $a.
    Code:
    PS> $a = 'ABC    69278  DEF      10278'
    PS> $a.Split(' ')
    ABC
    
    
    
    69278
    
    DEF
    
    
    
    
    
    10278
    PS> $a.Split(' ', [System.StringSplitOptions]::RemoveEmptyEntries)
    ABC
    69278
    DEF
    10278
    PS> -split $a
    ABC
    69278
    DEF
    10278
    PS> (-split $a)[2]
    DEF
    PS>

    Btw, are you going to get back to your previous thread?
      My Computer


  5. Posts : 11
    Windows 10, Debian 9
    Thread Starter
       #5

    Pyprohly said:
    Btw, are you going to get back to your previous thread?
    I saw your reply, but forgot to mark as solved. Instead of escaping everything as admin, I wrote a simple batch script which can be executed as admin. I'm used to the Linux way, but I need to learn the right and wrong way to do things in Windows. I appreciate everyone's help!
      My Computer


  6. Posts : 93
    Windows 10
       #6

    Windows is a very different beast to tame than Linux.

    If youíre more comfortable with it you can run bash shell scripts on Windows you know.

    But good on you for wanting to picking up the new skills.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 04:07.
Find Us




Windows 10 Forums