Author Posts

April 19, 2016 at 9:05 am

Hi there, so i have this variable that has the following data:
Region1|BU1|Site1
Region1|BU1|Site2
Region1|BU2|Site3
Region1|BU2|Site4
Region1|BU2|Site5
Region1|BU2|Site6
Region1|BU2|Site7
Region1|BU2|Site8
Region1|BU3|Site9
Region1|BU4|Site10
Region1|BU4|Site11
Region1|BU4|Site12
What I would like to do is to trim the last part, which is the |Site# – I've tried few things and i just cannot get it right, I can do foreach and then do split and re-assemble it again only with the first two parts, but i'm thinking (since everything's possible in powershell 🙂 ) there's gotta be a better way.
At the end I just need Region1|BU# – i don't mind if they repeat, i can do unique and that will take care if it.
Thanks in advance!

April 19, 2016 at 9:14 am

$a = @(
'Region1|BU1|Site1'
'Region1|BU1|Site2'
'Region1|BU2|Site3'
'Region1|BU2|Site4'
'Region1|BU2|Site5'
'Region1|BU2|Site6'
'Region1|BU2|Site7'
'Region1|BU2|Site8'
'Region1|BU3|Site9'
'Region1|BU4|Site10'
'Region1|BU4|Site11'
'Region1|BU4|Site12'
)

$a -replace '\|[^\|]+$'

April 19, 2016 at 9:18 am

Whoa, that was fast! Thank you Sir!

April 19, 2016 at 9:38 am

Will the length of the data be the same ? 6-1-3 ?
If so use Substring(0,9)

If the size isn't always the same, youll have to use LastIndexOf("|") and then
use substring but deduct 1 from the value of LastIndexOf("|")

can always try RegEx but dont have to go that path for every single string manipulation