none
Using split() to get LAST field of a string

    Question

  • Hi people

    I'm using the following (crude) method of getting a line from a block of text to extract the Primary Host Header from IIS based on W3SVC id. At the moment I'm doing this;

    $server = "192.168.0.1"
    $iiswebQuery = iisweb /query /s $server
    [string]$thisSiteName = $iiswebQuery | Select-String -pattern "W3SVC/100"

    Which gives a nasty-as-you-like string full of whitespace. So, we'll get rid of that;

    [string]$thisSiteName = $iiswebQuery | Select-String -pattern "W3SVC/100" |  %{$_ -replace '  *', ' '}

    Which gets rid of the multiple spaces and creates a string I can split. However, I want the last field which, depending on how many spaces are in the IIS site name, can vary in index. So, is there any way I can use Split() to get me the *last* field of the string?

    Yes, I know this is awflulness at its most, but I don't know how to query IIS through Powershell directly so I'm bodging the thing together.

    Any solutions (or complete alternatives) would be much appreciated. It needs to be able to run remotely rather than on localhost.

    Cheers muchly.
    O.
    Monday, May 19, 2008 4:09 PM

Answers

  • As soon as you use split, an array is created of the results.  If you just need the last "section":

    PSH>$test="some long string with spaces"
    PSH>$test.split()[-1]
    spaces
    PSH>

    Marco
    Wednesday, May 21, 2008 12:22 PM
    Moderator

All replies

  • I had a thought about this, I could address it by index directly if I know how many spaces were in the string. 7 spaces would equal 8 fields, therefore I could reference Music.

    Which begs the question, is there any way of counting how many iterations of a character (space, in this case) are in a string?

    Cheers
    O.
    Tuesday, May 20, 2008 11:42 AM
  • As soon as you use split, an array is created of the results.  If you just need the last "section":

    PSH>$test="some long string with spaces"
    PSH>$test.split()[-1]
    spaces
    PSH>

    Marco
    Wednesday, May 21, 2008 12:22 PM
    Moderator
  • $thisSiteName = $iiswebQuery | Select-String -pattern "W3SVC/100" |  %{$_ -replace '  *', ' '}

    new-item -name tmp.txt -type file -force
    $thisSiteName -split " " > tmp.txt
    $AllFields = get-content -path ./tmp.txt
    $FirstField = $AllFields[1] 

    #All Other Fields are available, too

    $LastField = $AllFields[($AllFields).Length]

    Wednesday, September 25, 2013 2:32 AM
  • As soon as you use split, an array is created of the results.  If you just need the last "section":

    PSH>$test="some long string with spaces"
    PSH>$test.split()[-1]
    spaces
    PSH>

    Marco
    Thanks :)

    Regards, Amol Patil

    Thursday, June 14, 2018 9:17 PM
  • If you use the -split operator, you can have any amount of spaces or tabs in between each word.

    -split 'hi    there     how        are' | select -last 1

    are




    • Edited by JS2010 Thursday, June 14, 2018 9:29 PM
    Thursday, June 14, 2018 9:29 PM
  • If you use the -split operator, you can have any amount of spaces or tabs in between each word.

    -split 'hi    there     how        are' | select -last 1

    are




    Nice.  I didn't see that.  A subtle undocumented trick because this doesn't work.

    'hi    there     how        are              '  -split ' ' |select -last 1


    \_(ツ)_/

    Thursday, June 14, 2018 9:35 PM
    Moderator
  • The default is something like this regex:

    'hi    there     how        are' -split '\s+'



    • Edited by JS2010 Thursday, June 14, 2018 9:38 PM
    Thursday, June 14, 2018 9:38 PM
  • Nope.

    PS D:\scripts> 'hi    there     how        are   ' -split '\s+' | select -last 1

    PS D:\scripts>

    If there are spaces at the end it will fail.


    \_(ツ)_/

    Thursday, June 14, 2018 9:56 PM
    Moderator
  • However - this works:

    'hi    there     how        are    '.split(' ',[System.StringSplitOptions]::RemoveEmptyEntries)|select -last 1


    \_(ツ)_/

    Thursday, June 14, 2018 10:02 PM
    Moderator