Windows 10: Powershell string parsing

Discus and support Powershell string parsing in Windows 10 Support to solve the problem; 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... Discussion in 'Windows 10 Support' started by jnich, May 2, 2017.

  1. jnich Win User

    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

    :)
     
    jnich, May 2, 2017
    #1

  2. Powershell - Need some help

    Hi Everyone,

    Currently I am pulling in data and parsing in powershell with a regex expression.

    $regex = [regex]
    "\b(?Powershell string parsing :(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"

    $ipAddress = $regex.matches($log) |
    % { $_.value }

    The variable becomes a string. I am looking for a way to convert the ip addresses into objects, that I can pass into a url.

    $ipAddress =
    $regex.matches($log) |
    % { $_.value } | ConvertFrom-StringData
    $IPAddress -OutBuffer
    $ipAddressURL

    This is the output currently shown when I run the $ipAddress variable in powershell. I have looked and googled left and right but I am unsure how to separate these into objects and not string in PS. Can someone help?

    118.101.203.252

    24.10.216.192

    173.252.100.117

    173.252.114.114
    Thanks!

    Third.
     
    thirddementia, May 2, 2017
    #2
  3. Network adapters not installing correctly or VPN's not installing properly on Win10?

    I think your clue could be that that string was given under the word "commands". So, I would suspect that two lines were given as one. We can try parsing it out. The tip you got to check the reg delete syntax could be a good place to start.

    Looks like the /va and /f would parse OK but the rest would not. So, I would try breaking off the rest into a second command, consistent with the impression given by the use of the word "commands".



    Good luck

    Robert Aldwinckle

    ---
     
    Robert Aldwinckle on forums, May 2, 2017
    #3
  4. pparks1 Win User

    Powershell string parsing

    pparks1, May 2, 2017
    #4
  5. jnich Win User
    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][/quote] I must Split twice
    Code: $a = "ABC 69278 DEF 10278" $a = $a.Split() -ne"" $a.Split()[2][/quote] I feel like I'm overcomplicating things and this could be simplified somehow. Is there a better way?
     
    jnich, May 2, 2017
    #5
  6. Pyprohly Win User
    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)[/quote]
    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>[/quote]
    Btw, are you going to get back to your previous thread?
     
    Pyprohly, May 2, 2017
    #6
  7. jnich Win User
    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!
     
    jnich, May 2, 2017
    #7
  8. Pyprohly Win User

    Powershell string parsing

    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.
     
    Pyprohly, Apr 4, 2018
    #8
Thema:

Powershell string parsing

Loading...
  1. Powershell string parsing - Similar Threads - Powershell string parsing

  2. Failed to parse WDG method

    in Windows 10 Gaming
    Failed to parse WDG method: I've been trying to dual boot Windows and Kali Linux multiple times in different ways now but I always get the same error while installing Kali. Once kali is installed and I boot into it, It loads for some time and sends me to a black screen with a white prompt and an error:...
  3. String OBJ_STATUS_DISKSPACE_SPACE was not found in string table

    in Windows 10 Gaming
    String OBJ_STATUS_DISKSPACE_SPACE was not found in string table: I'm trying to complete the installation of the BS-240 clinical chemistry analyzer that is to be connected to a Windows 10 computer. Whenever I run the 'setup.exe' file to start the installation, I see this error pop up immediately - String OBJ_STATUS_DISKSPACE_SPACE was not...
  4. String OBJ_STATUS_DISKSPACE_SPACE was not found in string table

    in Windows 10 Software and Apps
    String OBJ_STATUS_DISKSPACE_SPACE was not found in string table: I'm trying to complete the installation of the BS-240 clinical chemistry analyzer that is to be connected to a Windows 10 computer. Whenever I run the 'setup.exe' file to start the installation, I see this error pop up immediately - String OBJ_STATUS_DISKSPACE_SPACE was not...
  5. Error parsing

    in Windows 10 BSOD Crashes and Debugging
    Error parsing: Can someone tell me what this is?Error parsingC:\Windows\Microsoft.NET\Framework64\v4.030319\config\machine.configParser returned error 0xC00CE508I haven't a clue. Is it a Trojan? If not can it be fixed? Don't know where it came from and it's driving me nuts popping up all...
  6. Parsing error

    in Windows 10 Installation and Upgrade
    Parsing error: I have an HP laptop running Windows 10 and I have started to get the following error message "Error Parsing C:\windows\microsoft.net\framework64\v4.0.30319\config\machine.config Parser returned error 0x8004005." Can you please help me....
  7. net parsing error

    in Windows 10 BSOD Crashes and Debugging
    net parsing error: how do I move the new machine config file to framework net https://answers.microsoft.com/en-us/windows/forum/all/net-parsing-error/ad01afea-2a8e-4521-a68e-240cda5d582d
  8. parse error 0x80070570

    in Windows 10 Customization
    parse error 0x80070570: how to fix error parsing c::\windows\micorsoft.net\Framework64\v4.0.30319\config\machine.confir Parser returned error 0x80070570 https://answers.microsoft.com/en-us/windows/forum/all/parse-error-0x80070570/32fdd189-38a6-45c5-8247-7b3fc2ed1f38
  9. Deleting Lines in a Text File Given Strings From Another File Using Set-String (Powershell)

    in AntiVirus, Firewalls and System Security
    Deleting Lines in a Text File Given Strings From Another File Using Set-String (Powershell): Basically what's happening is that I have two files, one with a list of allowed users and another list of users that are actually on the desktop. What I'm trying to do is use a for loop to grab each name in the AllowedUsers.txt file, and use Select-String to find any names...
  10. Parsing Error 508

    in Windows 10 BSOD Crashes and Debugging
    Parsing Error 508: When I open Quicken 2018 latest version and move around to other screens, I get this error message: Error Parsing, C:\Windows\Microsoft.net\framework64\v4.0.30319\config\machine.config . Parser returned error OxC00CE508....