Keep Left Side of Split

Hello Everybody :wink:

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 :

 

$Srv = "Master_Server.fomrum.org_123456789_C1_R1.IMG"
$Srv.Split("_")[-1] = R1.img

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”

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.

$Srv -replace "_R.*", ""

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.

 

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:

$Srv = "Master_Server.fomrum.org_123456789_C1_R1.IMG"
$temp = ($srv -split '_')[0..$($spl.Count - 2)] -join '_'
$temp

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.

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

$m = [regex]::Match($srv,'(?<left>Master_Server.forum.org_\d+_C1)_(?<right>.+)')
$m.Groups['left'].value
$m.Groups['right'].value

Output:

Master_Server.forum.org_123456789_C1
R1.IMG

 

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 :

Function Extract-String {
    Param(
        [Parameter(Mandatory=$true)][string]$string
        , [Parameter(Mandatory=$true)][char]$character
        , [Parameter(Mandatory=$false)][ValidateSet("Right","Left")][string]$range
        , [Parameter(Mandatory=$false)][int]$afternumber
        , [Parameter(Mandatory=$false)][int]$tonumber
    )
    Process
    {
        [string]$return = ""

        if ($range -eq "Right")
        {
            $return = $string.Split("$character")[($string.Length - $string.Replace("$character","").Length)]
        }
        elseif ($range -eq "Left")
        {
            $return = $string.Split("$character")[0]
        }
        elseif ($tonumber -ne 0)
        {
            for ($i = $afternumber; $i -le ($afternumber + $tonumber); $i++)
            {
                $return += ($string.Split("$character")[$i]+"$character") #A modifier selon le besoin.
            }
        }
        else
        {
            $return = $string.Split("$character")[$afternumber]
        }

        return $return
    }
}

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 :

 

"Master_Server.Suffix_C<1..10>_<F | HDR>_<Could have: R><number>.<info | img | fmk>"
ex: 
"Master_Server_forum.org_C1_HDR_R1.info"
"Master_Server_forum.org_C2_F1_R1.fmk"
"Master_Server_forum.org_C1_F2.info"

etc...

Really string before the first split :

2|Master_ServerRIE.forum.org|Orph-Policy-Lab|Master_ServerRIE.forum.org_1606839714_C2_F12_R1.img|397fcba57ed5bf20055a121f33cf2cb5a3e7a4f2dab2af0aa8c159e657a76752|01a7bcd4693993abcadb10c908ff4ae5|2048
2|Master_ServerRIE.forum.org|Orph-Policy-Lab|Master_ServerRIE.forum.org_1606839714_C2_F12_R1.hdr|ffb73c40eef40ef9f9b1889c3f06289834bd48cccb2866f2ee2e3002c792f718|01b69bdf2842776cbc695b4755df19b3|2048
2|Master_ServerRIE.forum.org|Orph-Policy-Lab|Master_ServerRIE.forum.org_1606839714_C2_F12_R1.map|6cc25cf936e7c78d4939ca3a73a9be74ccc301aecc27c670c8e80ee0061fc8bb|011da33157fd1a431c7bd64fba759d8f|2048
2|Master_ServerRIE.forum.org|Orph-Policy-Lab|Master_ServerRIE.forum.org_1606839950_C1_HDR.info|3d82790c44465e1b2bd29e7e057e2eed73fbfa1f421c05f1a16919c80e49fe30|01ec16aca18b54a3c46a63966aba6f52|1025
2|Master_ServerRIE.forum.org|Orph-Policy-Lab|Master_ServerRIE.forum.org_1606839950_C1_HDR.img|0ef12acea3c8df6389efbd92741cdcf45b55b1c8c2decc27086231aeaba28530|01586f6f270bb16a63f7b75bb0d78c80|1025
2|Master_ServerRIE.forum.org|Orph-Policy-Lab|Master_ServerRIE.forum.org_1606839950_C1_F1.fmk|c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a|019b18769fcdcc1b9f91824afecb30bd|1
2|Master_ServerRIE.forum.org|Orph-Policy-Lab|Master_ServerRIE.forum.org_1606839950_C1_F1.info|45607f58cfffd4ee36f12220ecb7766c99f8d83c0238fd6d0ccad43776d24365|01ed5041abd8e0966cc1ba9d9c7e0bee|1025
2|Master_ServerRIE.forum.org|Orph-Policy-Lab|Master_ServerRIE.forum.org_1606839950_C1_F1.img|c623792a4c8f50dd1111fc24f84c3f88dc36b62a0e7fa0776111b7bad499010c|011bf1d5bf1879dab880031c112aafb1|1026
2|Master_ServerRIE.forum.org|Orph-Policy-Lab|Master_ServerRIE.forum.org_1606839950_C1_F1.hdr|30c3e699698eea438b09ab70875bfd10a092051fcaaa0f03c23acad40c1275c0|01a70d9a44659ba5a121f3401741e30f|1026
2|Master_ServerRIE.forum.org|Orph-Policy-Lab|Master_ServerRIE.forum.org_1606839950_C1_F1.map|0ce9fbb8ef276115291ffa651b9f654f71469586d0185f54f959fd28da2c1f8a|01636e64a08d5c411f5de892a2de7037|1026
2|Master_ServerRIE.forum.org|Orph-Policy-Lab|Master_ServerRIE.forum.org_1606839950_C1_F2.info|109434866adb46850d50d3cf36c3560025cfb1b412c595de7897892c800a5538|014866a5c9ff69bf196dd88917f62a67|1026
2|Master_ServerRIE.forum.org|Orph-Policy-Lab|Master_ServerRIE.forum.org_1606839950_C1_F2.img|bfacce51c65280e27628aae49e2a99f32b38f0c7e7e2ecf9e1430918efa39a8c|017e61c510dd05e763e9fa665cdba593|1026
2|Master_ServerRIE.forum.org|Orph-Policy-Lab|Master_ServerRIE.forum.org_1606839950_C1_F2.hdr|14374750681394d525ddc2f1ea096f1cef78543360360cae3b1890d6cd8bb65d|01c3fc58650cdc4b728080d9211a7931|1026
2|Master_ServerRIE.forum.org|Orph-Policy-Lab|Master_ServerRIE.forum.org_1606839950_C1_F2.map|134e8860fe0adae8a56aeef06afe83a043dc4eec2ed6b2d75741336741251ec1|011d728b9797639c4b34d101c60177c0|1026

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 :wink:
thx you all :wink:

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

 

And yet another way …

$String = ‘Master_ServerRIE.forum.org_1606839714_C2_F12_R1.img’

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

Thx to you ^^

And have a nie Merry Christmas !