Keep Left Side of Split

Welcome Forums General PowerShell Q&A Keep Left Side of Split

Viewing 6 reply threads
  • Author
    Posts
    • #276171
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      Hello Everybody 😉

      My Issue is this :

      I have a string like this : master_server.forum.org_123456789_C1_R1.img

      The string after the “_C” can change !

      I need to have just “Master_Server.forum.org_C1” but with split i dont success to have it.

      For the momment i have try some command like :

       

      So i have exactly the Right Side but how to have the left side ?
      Can we do the split but begining by the right side and not the Left side to don’ttake car about the name of the server ?
      And to get the string “Master_Server.forum.org_123456789_C1”

      • This topic was modified 1 month, 2 weeks ago by Renaud.
      • This topic was modified 1 month, 2 weeks ago by Renaud.
    • #276198
      Participant
      Topics: 5
      Replies: 252
      Points: 998
      Helping Hand
      Rank: Major Contributor

      Split creates an array of strings.  Sounds like you just need part of the original string.  I would recommend the -replace operator instead of -split.

      The first value “_R.*” is a regular expression, so modify it as needed for your dataset.  I was just working with the example you posted.

       

      • This reply was modified 1 month, 2 weeks ago by Mike R..
    • #276255
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,987
      Helping Hand
      Rank: Community Hero

      Basically you’re trying to remove the last element of an array, which is more painful that it needs to be. Here is a way to do it:

      After the string is split, we can get index 0 to the array count minus 2. Count starts at 1, not 0 which is -1 and then the last element we want to remote is -1, for a total of 2. Then you glue it back together with underscore.

    • #276321
      Participant
      Topics: 4
      Replies: 425
      Points: 748
      Helping Hand
      Rank: Major Contributor

      Another way to do it is with a regular expression.  Capture the left and right portions into named capture groups.

       

    • #276495
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      Hello all ^^

       

      Thx for your reply and Woaw it’s Works fine for this string.

      I had  finish a Script for a client and they said after the name of the server was not “MasterServer” but “Master_Server…..”

      In this time i have found a script who works but not all the time  :

      Sorry for those who see my first reply, i was too happy by your reply and forget  many important things i haven’t speak about, sorry !

      The string is this :

       

      Really string before the first split :

      So i take the 4 column with the .Split(“|”)[3] , so i get this :

      “Master_ServerRIE.forum.org_1606839714_C2_F12_R1.img”

      after i have to take the string to _C?_?? and would like to have it but without take care of the name of the server, if there is an “-” or not.

      So i must Keep : “Master_ServerRIE.forum.org_1606839714_C2_F12”, but the name of the server could be MasterServerRIE or something else.

      I realise, it’s bad that i have done here i put a first thin and after  a more tricky things…

      I’will work on it with the script you have done.

      So sorry  !

      Any way, Have a nice day Dude 😉
      thx you all 😉

      And i would like to thx you again for all the help i can have on this forums !

       

      • This reply was modified 1 month, 2 weeks ago by Renaud.
      • This reply was modified 1 month, 2 weeks ago by Renaud.
      • This reply was modified 1 month, 2 weeks ago by Renaud.
    • #276708
      Participant
      Topics: 10
      Replies: 201
      Points: 987
      Helping Hand
      Rank: Major Contributor

      And yet another way ….

      $String = 'Master_ServerRIE.forum.org_1606839714_C2_F12_R1.img'

      $NewString = $String.Substring(1, $String.LastIndexOf('_') - 1)

    • #278292
      Participant
      Topics: 12
      Replies: 62
      Points: 190
      Rank: Participant

      Thx to you ^^

      And have a nie Merry Christmas !

Viewing 6 reply threads
  • You must be logged in to reply to this topic.