Windows Vista Forums
Vista Forums Home Join Vista Forums Donate 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 Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

split works much different than expected

Reply
 
LinkBack Thread Tools Display Modes
Old 05-22-2007   #1 (permalink)
Frank
Guest


 
 

split works much different than expected

Hi,

I am trying to get the error codes from a file but split seems to work much
different than I expect. ie, my file error_log contains:

cmd.exe exited on testserver with error code 1603.

when I try to do a split ie.

$testerr = get-content error_log | where($_ -match "error code"}
if ($testerr){
($one,$two)=$testerr.split("error code")
"one: $one, two: $two"
}

I get really strange output, like:

one: ps, two: x . x : .......

Can someone help with split?

Thanks,


My System SpecsSystem Spec
Old 05-22-2007   #2 (permalink)
Brandon Shell
Guest


 
 

Re: split works much different than expected

Hopefully this will enlighten you a bit:
http://msdn2.microsoft.com/en-us/lib...ing.split.aspx

Maybe a regex would be better for you.

"Frank" <Frank@discussions.microsoft.com> wrote in message
news:AA3C2345-7E77-4C73-A7D9-F3E73B1F6790@microsoft.com...
> Hi,
>
> I am trying to get the error codes from a file but split seems to work
> much
> different than I expect. ie, my file error_log contains:
>
> cmd.exe exited on testserver with error code 1603.
>
> when I try to do a split ie.
>
> $testerr = get-content error_log | where($_ -match "error code"}
> if ($testerr){
> ($one,$two)=$testerr.split("error code")
> "one: $one, two: $two"
> }
>
> I get really strange output, like:
>
> one: ps, two: x . x : .......
>
> Can someone help with split?
>
> Thanks,
>


My System SpecsSystem Spec
Old 05-22-2007   #3 (permalink)
Jean
Guest


 
 

Re: split works much different than expected

> Hi,
>
> I am trying to get the error codes from a file but split seems to work much
> different than I expect. ie, my file error_log contains:
>
> cmd.exe exited on testserver with error code 1603.
>
> when I try to do a split ie.
>
> $testerr = get-content error_log | where($_ -match "error code"}
> if ($testerr){
> ($one,$two)=$testerr.split("error code")
> "one: $one, two: $two"
> }
>
> I get really strange output, like:
>
> one: ps, two: x . x : .......
>
> Can someone help with split?
>
> Thanks,


See this thread :

http://groups.google.com/group/micro...e96d7be3a45ff7

When Split is used with one argument, argument is a Char not a String.
To use a String as argument you could do something like:

$testerr.Split([string[]]'error code',[StringSplitOptions]::None)

Regards,

--
Jean - JMST
Belgium


My System SpecsSystem Spec
Old 05-22-2007   #4 (permalink)
Frank
Guest


 
 

Re: split works much different than expected

Thanks everyone for responding. My last question is, the string I get left is:

"1603." in variable $code.

I want to remove the "." so I tried: $code = $code -replaced ".",""

but I get a blank variable. Can someone tell me why?

thanks,




"Jean" wrote:

> > Hi,
> >
> > I am trying to get the error codes from a file but split seems to work much
> > different than I expect. ie, my file error_log contains:
> >
> > cmd.exe exited on testserver with error code 1603.
> >
> > when I try to do a split ie.
> >
> > $testerr = get-content error_log | where($_ -match "error code"}
> > if ($testerr){
> > ($one,$two)=$testerr.split("error code")
> > "one: $one, two: $two"
> > }
> >
> > I get really strange output, like:
> >
> > one: ps, two: x . x : .......
> >
> > Can someone help with split?
> >
> > Thanks,

>
> See this thread :
>
> http://groups.google.com/group/micro...e96d7be3a45ff7
>
> When Split is used with one argument, argument is a Char not a String.
> To use a String as argument you could do something like:
>
> $testerr.Split([string[]]'error code',[StringSplitOptions]::None)
>
> Regards,
>
> --
> Jean - JMST
> Belgium
>
>
>

My System SpecsSystem Spec
Old 05-22-2007   #5 (permalink)
Brandon Shell
Guest


 
 

Re: split works much different than expected

$code = $code.replace(".",$null)

"Frank" <Frank@discussions.microsoft.com> wrote in message
news:62B74980-D884-4A8D-99AC-11003031F6CE@microsoft.com...
> Thanks everyone for responding. My last question is, the string I get
> left is:
>
> "1603." in variable $code.
>
> I want to remove the "." so I tried: $code = $code -replaced ".",""
>
> but I get a blank variable. Can someone tell me why?
>
> thanks,
>
>
>
>
> "Jean" wrote:
>
>> > Hi,
>> >
>> > I am trying to get the error codes from a file but split seems to work
>> > much
>> > different than I expect. ie, my file error_log contains:
>> >
>> > cmd.exe exited on testserver with error code 1603.
>> >
>> > when I try to do a split ie.
>> >
>> > $testerr = get-content error_log | where($_ -match "error code"}
>> > if ($testerr){
>> > ($one,$two)=$testerr.split("error code")
>> > "one: $one, two: $two"
>> > }
>> >
>> > I get really strange output, like:
>> >
>> > one: ps, two: x . x : .......
>> >
>> > Can someone help with split?
>> >
>> > Thanks,

>>
>> See this thread :
>>
>>
>> http://groups.google.com/group/micro...e96d7be3a45ff7
>>
>> When Split is used with one argument, argument is a Char not a String.
>> To use a String as argument you could do something like:
>>
>> $testerr.Split([string[]]'error code',[StringSplitOptions]::None)
>>
>> Regards,
>>
>> --
>> Jean - JMST
>> Belgium
>>
>>
>>


My System SpecsSystem Spec
Old 05-23-2007   #6 (permalink)
Ryan Milligan
Guest


 
 

Re: split works much different than expected

If you're wondering why your original code didn't work, it's because
the -replace operator uses regular expressions, and "." is a special
character in regular expressions that means "match any character." So, when
you regex-replace "." with the empty string, you're replacing every
character in the string with nothing. You can escape it by using "\."
instead of just ".", but in this case, I'd recommend just using
String.Replace() as suggested by Brandon.

Also as Brandon suggested, a regular expression might serve you better here.
You could try something like this:

gc error_log | foreach {([regex]"(?<app>\w+) exited with error code
(?<error>\d+)\.").Match($_).Groups["error"].Value}

This will give you all the error codes in your file that are in this format.
You could also return a hashtable that includes the name of the app and the
error message -- I only included that to show that you can capture more
information this way than just the error code. Hope this helps.

-- Ryan Milligan

"Brandon Shell" <tshell.mask@mk.gmail.com> wrote in message
news:%23RuPoROnHHA.596@TK2MSFTNGP06.phx.gbl...
> $code = $code.replace(".",$null)
>
> "Frank" <Frank@discussions.microsoft.com> wrote in message
> news:62B74980-D884-4A8D-99AC-11003031F6CE@microsoft.com...
>> Thanks everyone for responding. My last question is, the string I get
>> left is:
>>
>> "1603." in variable $code.
>>
>> I want to remove the "." so I tried: $code = $code -replaced ".",""
>>
>> but I get a blank variable. Can someone tell me why?
>>
>> thanks,
>>
>>
>>
>>
>> "Jean" wrote:
>>
>>> > Hi,
>>> >
>>> > I am trying to get the error codes from a file but split seems to work
>>> > much
>>> > different than I expect. ie, my file error_log contains:
>>> >
>>> > cmd.exe exited on testserver with error code 1603.
>>> >
>>> > when I try to do a split ie.
>>> >
>>> > $testerr = get-content error_log | where($_ -match "error code"}
>>> > if ($testerr){
>>> > ($one,$two)=$testerr.split("error code")
>>> > "one: $one, two: $two"
>>> > }
>>> >
>>> > I get really strange output, like:
>>> >
>>> > one: ps, two: x . x : .......
>>> >
>>> > Can someone help with split?
>>> >
>>> > Thanks,
>>>
>>> See this thread :
>>>
>>>
>>> http://groups.google.com/group/micro...e96d7be3a45ff7
>>>
>>> When Split is used with one argument, argument is a Char not a String.
>>> To use a String as argument you could do something like:
>>>
>>> $testerr.Split([string[]]'error code',[StringSplitOptions]::None)
>>>
>>> Regards,
>>>
>>> --
>>> Jean - JMST
>>> Belgium
>>>
>>>
>>>

>



My System SpecsSystem Spec
Reply

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
When is the next update expected? Any news? KonstantinDK Live Mail 3 04-12-2008 06:37 PM
Expected release date for PS 2.0? Juan Tran PowerShell 2 02-26-2008 07:17 AM
When is a new release expected? Seansan Live Mail 4 01-30-2008 09:37 PM
base score lower than expected Paul Pedersen Vista General 17 06-29-2007 01:19 AM
April CTP Expected in the next few weeks Andre Da Costa [Extended64] Vista General 7 04-12-2006 03:00 AM


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 47 48 49 50 51 52 53