1.    02 May 2017 #1
    Join Date : Apr 2017
    Posts : 11
    Windows 10, Debian 9

    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 ComputerSystem Spec
  2.    02 May 2017 #2
    Join Date : Oct 2014
    Posts : 920
    Windows 10 Pro
      My ComputerSystem Spec
  3.    02 May 2017 #3
    Join Date : Apr 2017
    Posts : 11
    Windows 10, Debian 9
    Thread Starter

    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 ComputerSystem Spec
  4.    03 May 2017 #4
    Join Date : Mar 2015
    Sydney, NSW
    Posts : 93
    Windows 10

    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 ComputerSystem Spec
  5.    03 May 2017 #5
    Join Date : Apr 2017
    Posts : 11
    Windows 10, Debian 9
    Thread Starter

    Quote Originally Posted by Pyprohly View Post
    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 ComputerSystem Spec
  6.    03 May 2017 #6
    Join Date : Mar 2015
    Sydney, NSW
    Posts : 93
    Windows 10

    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 ComputerSystem Spec

 


Similar Threads
Thread Forum
String with spaces in set /p (batch file)
Hi! I would like to let a user type in things with spaces in set /p. This is the code I currently have that does not work: :newdoc2 cls echo What do you want to say? set saynew=%randomnum% set /p saynew= if %saynew% == %randomnum% goto...
Software and Apps
dmis string
I ran an sfc/scannow and it revealed corrupted files that could not be fixed. I tried to run a dmis but could not get the source correct. I upgraded to windows 10 and don'y know what the correct string is. Please help! Thanks, Harvey Stobezki
AntiVirus, Firewalls and System Security
Looking for a Text to string application
Hello I'm looking for an application or a web site that I will use to record good quality mp3 file from a string. Type a text and the output is a spoken mp3 file I was using few years ago AT&T application, but this site is down now Martin
Software and Apps
Build Number String?
What's the final full build number string for Windows 10?
Windows Insider
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 18:12.
Find Us
Twitter Facebook Google+ Ten Forums iOS App Ten Forums Android App



Windows 10 Forums