Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - Looking for something better than split and join

Reply
 
Old 10-21-2008   #1 (permalink)
Andy
Guest


 
 

Looking for something better than split and join

I have a string that looks like "/vol/InfraDev_AppDev03/VHD.lun"
I need to do something so I can get

$vol = "/vol/InfraDev_AppDev03/"
$lun = "VHD.lun"

My first solution (which works but I just think its not so elegant)
is to split the string on "/" and then do a join on "/" with the first
two elements in the result of the split and set that to $vol, and then
leave the third element as $lun.

I am no regex kung fu master, but I was wondering if there was a
slightly more elegant way to solve this problem.

Thanks,

Andy

My System SpecsSystem Spec
Old 10-21-2008   #2 (permalink)
Shay Levy [MVP]
Guest


 
 

Re: Looking for something better than split and join

Hi Andy,

An alternative is to use the split-path cmdlet:

PS > split-path "/vol/InfraDev_AppDev03/VHD.lun" -leaf
VHD.lun


The problem is that split-path replaces each slash with a backslash in case
of this:

PS > split-path "/vol/InfraDev_AppDev03/VHD.lun" -Parent
\vol\InfraDev_AppDev03



A safer solution is a combination of substring() and lastIndexOf() methods:

PS > $p.substring($p.lastIndexOf("/")+1)
VHD.lun


PS > $p.substring(0,$p.lastIndexOf("/")+1)
/vol/InfraDev_AppDev03


---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar



A> I have a string that looks like "/vol/InfraDev_AppDev03/VHD.lun" I
A> need to do something so I can get
A>
A> $vol = "/vol/InfraDev_AppDev03/"
A> $lun = "VHD.lun"
A> My first solution (which works but I just think its not so elegant)
A> is to split the string on "/" and then do a join on "/" with the
A> first
A> two elements in the result of the split and set that to $vol, and
A> then
A> leave the third element as $lun.
A> I am no regex kung fu master, but I was wondering if there was a
A> slightly more elegant way to solve this problem.
A>
A> Thanks,
A>
A> Andy
A>


My System SpecsSystem Spec
Old 10-21-2008   #3 (permalink)
Andy
Guest


 
 

Re: Looking for something better than split and join

On Oct 21, 9:28*am, Shay Levy [MVP] <n...@xxxxxx> wrote:
Quote:

> Hi Andy,
>
> An alternative is to use the split-path cmdlet:
>
> PS > split-path "/vol/InfraDev_AppDev03/VHD.lun" -leaf
> VHD.lun
>
> The problem is that split-path replaces each slash with a backslash in case
> of this:
>
> PS > split-path "/vol/InfraDev_AppDev03/VHD.lun" -Parent
> \vol\InfraDev_AppDev03
>
> A safer solution is a combination of substring() and lastIndexOf() methods:
>
> PS > $p.substring($p.lastIndexOf("/")+1)
> VHD.lun
>
> PS > $p.substring(0,$p.lastIndexOf("/")+1)
> /vol/InfraDev_AppDev03
>
> ---
> Shay Levy
> Windows PowerShell MVPhttp://blogs.microsoft.co.il/blogs/ScriptFanatic
> PowerShell Toolbar:http://tinyurl.com/PSToolbar
>
> A> I have a string that looks like "/vol/InfraDev_AppDev03/VHD.lun" I
> A> need to do something so I can get
> A>
> A> $vol = "/vol/InfraDev_AppDev03/"
> A> $lun = "VHD.lun"
> A> My first solution (which works but I just think its not so elegant)
> A> is to split the string on "/" and then do a join on "/" with the
> A> first
> A> two elements in the result of the split and set that to $vol, and
> A> then
> A> leave the third element as $lun.
> A> I am no regex kung fu master, but I was wondering if there was a
> A> slightly more elegant way to solve this problem.
> A>
> A> Thanks,
> A>
> A> Andy
> A>
Thanks Shay, much appreciated

Andy
My System SpecsSystem Spec
Old 10-22-2008   #4 (permalink)
Tao Ma
Guest


 
 

Re: Looking for something better than split and join

Hi Andy,

Shay levy has already answered your question.

I paste a more tricky way to solve your problem:

PS C:\> $s = "/vol/InfraDev_AppDev03/VHD.lun"
PS C:\> $vol, $lun = ($s -replace '/([^/]*)$',"/`n`$1").split("`n")
PS C:\> $vol
/vol/InfraDev_AppDev03/
PS C:\> $lun
VHD.lun

The core of my code is ($s -replace '/([^/]*)$',"/`n`$1").split("`n"). The
regular expression inserts an new line character after the last slash. Then
split() splits the string using new line as the separator. Finally, assign
the two parts returned from the split() to the variable vol and lun
separately.

Best wishes,
Tao Ma

"Andy" <andy@xxxxxx-powershell.com> ????
news:1f1cd9f8-1c44-4666-87ed-562e4f9f6d37@xxxxxx
On Oct 21, 9:28 am, Shay Levy [MVP] <n...@xxxxxx> wrote:
Quote:

> Hi Andy,
>
> An alternative is to use the split-path cmdlet:
>
> PS > split-path "/vol/InfraDev_AppDev03/VHD.lun" -leaf
> VHD.lun
>
> The problem is that split-path replaces each slash with a backslash in
> case
> of this:
>
> PS > split-path "/vol/InfraDev_AppDev03/VHD.lun" -Parent
> \vol\InfraDev_AppDev03
>
> A safer solution is a combination of substring() and lastIndexOf()
> methods:
>
> PS > $p.substring($p.lastIndexOf("/")+1)
> VHD.lun
>
> PS > $p.substring(0,$p.lastIndexOf("/")+1)
> /vol/InfraDev_AppDev03
>
> ---
> Shay Levy
> Windows PowerShell MVPhttp://blogs.microsoft.co.il/blogs/ScriptFanatic
> PowerShell Toolbar:http://tinyurl.com/PSToolbar
>
> A> I have a string that looks like "/vol/InfraDev_AppDev03/VHD.lun" I
> A> need to do something so I can get
> A>
> A> $vol = "/vol/InfraDev_AppDev03/"
> A> $lun = "VHD.lun"
> A> My first solution (which works but I just think its not so elegant)
> A> is to split the string on "/" and then do a join on "/" with the
> A> first
> A> two elements in the result of the split and set that to $vol, and
> A> then
> A> leave the third element as $lun.
> A> I am no regex kung fu master, but I was wondering if there was a
> A> slightly more elegant way to solve this problem.
> A>
> A> Thanks,
> A>
> A> Andy
> A>
Thanks Shay, much appreciated

Andy

My System SpecsSystem Spec
Old 10-23-2008   #5 (permalink)
Flowering Weeds
Guest


 
 

Re: Looking for something better than split and join

Quote:

>
> The problem is that split-path
> replaces each slash with a
> backslash in case of this:
>
> PS > split-path "/vol/InfraDev_AppDev03/VHD.lun" -Parent
> \vol\InfraDev_AppDev03
>
Mmm data parsing!

Perhaps do it the same way
in any Windows process!

So using the _automation_ tool,
powershell.exe:

$path = "/vol/InfraDev_AppDev03/VHD.lun"

$path | LogParser.exe "SELECT
EXTRACT_FILENAME(text) AS Filename,
STRCAT(
EXTRACT_PATH(text), '/') AS Path
FROM STDIN " -i textline -stats off

Returns:

Filename Path
-------- ----------------------
VHD.lun /vol/InfraDev_AppDev03/

Have some data parsing fun,
Log Parser it!


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
LINQ: Inner Join & Left Outer Join Q .NET General
Split text VB Script
Downloads are getting split in two Vista General
Split Desktop Vista performance & maintenance


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46