Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

[regex]::Split help

Closed Thread
 
Thread Tools Display Modes
Old 11-10-2006   #1 (permalink)
Marco Shaw
Guest


 

[regex]::Split help

I have this file:

PS C:\> gc test1.csv
marco,111
shaw,222

I *only* want to ouput the 1st part of each line. In other words, I just
want this to show in the output:

marco
shaw

Tried this, but it doesn't quite work:
PS C:\> gc test1.csv|foreach {[regex]::Split(($_),"`,")}
marco
111
shaw
222
PS C:\>


Old 11-10-2006   #2 (permalink)
dreeschkind
Guest


 

RE: [regex]::Split help

"Marco Shaw" wrote:

> I have this file:
>
> PS C:\> gc test1.csv
> marco,111
> shaw,222
>
> I *only* want to ouput the 1st part of each line. In other words, I just
> want this to show in the output:
>
> marco
> shaw


This is another example which shows that you often don't need regex for
simple tasks:

PS> gc test1.csv | foreach { $_.split(',')[0] }
marco
shaw

Also note that if your csv file had a header, you could simply use
import-csv cmdlet:

PS> gc test1.csv
name,value
marco,111
shaw,222

PS> import-csv test1.csv

name
value
----
-----
marco 111
shaw 222

PS> import-csv test1.csv | % {$_.name}
marco
shaw

--
greetings
dreeschkind
Old 11-10-2006   #3 (permalink)
mgrogan@gia.edu
Guest


 

Re: ::Split help

I truly wish import-csv could handle csv files without headers. Maybe
it could come up with default field names or something...

>
> Also note that if your csv file had a header, you could simply use
> import-csv cmdlet:
>
> PS> gc test1.csv
> name,value
> marco,111
> shaw,222
>
> PS> import-csv test1.csv
>
> name
> value
> ----
> -----
> marco 111
> shaw 222
>
> PS> import-csv test1.csv | % {$_.name}
> marco
> shaw
>
> --
> greetings
> dreeschkind


Old 11-10-2006   #4 (permalink)
Flowering Weeds
Guest


 

Re: ::Split help


> I truly wish import-csv could handle
> csv files without headers. Maybe
> it could come up with default field
> names or something...
>


Perhaps use Microsoft's Log Parser
(for sure if the files are large):

An TSV demo:

PS> netstat -ano

Active Connections

Proto Local Address Foreign Address State
PID
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING
848
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
4
(...)

See the headers shown above?

PS> netstat -ano |
>> LogParser "SELECT * FROM STDIN" -i:TSV `
>> -iSeparator:space -nSep:2 -fixedSepff `
>> -nSkipLines:3
>>

Filename RowNumber Proto Local Address Foreign Address State
PID
-------- --------- ----- ----------------- --------------- --------- ------
STDIN 5 TCP 0.0.0.0:135 0.0.0.0:0 LISTENING
848
STDIN 6 TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4
(...)

Now to get rid of Log Parsers headers
shown above just add -headersff

PS> netstat -ano |
>> LogParser "SELECT * FROM STDIN" -i:TSV `
>> -iSeparator:space -nSep:2 -fixedSepff `
>> -nSkipLines:3 -headersff
>>

STDIN 5 TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 848
STDIN 6 TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4

Now get the fields you _only_ want:

PS> netstat -ano |
>> LogParser "SELECT Proto, State, Pid `
>> From STDIN" -i:TSV -iSeparator:space `
>> -nSep:2 -fixedSepff -nSkipLines:3 `
>> -headersff
>>

TCP LISTENING 848
TCP LISTENING 4
(...)

A CSV file:

PS> get-content testnames.csv |
>> LogParser "SELECT * `
>> From STDIN" -i:CSV -headersff
>>

STDIN 2 Bob Smith 123 Main Any The State 23456
STDIN 3 Mary Jones 333 Maple None What State 66545
STDIN 4 Gerry Who 345 Dead End Road Big City Small State 22434

PS> get-content testnames.csv |
>> LogParser "SELECT City,State `
>> From STDIN" -i:CSV -headersff
>>

Any The State
None What State
Big City Small State

Statistics:
-----------
Elements processed: 3
Elements output: 3
Execution time: 0.00 seconds

Try other Log Parser options too!




Old 11-11-2006   #5 (permalink)
Andrew Watt [MVP]
Guest


 

Re: ::Split help

On 10 Nov 2006 09:41:51 -0800, mgrogan@gia.edu wrote:

>I truly wish import-csv could handle csv files without headers. Maybe
>it could come up with default field names or something...


Hi,

This function should do what you need, with a little adaptation
perhaps.

I have put comments in it so it should be fairly easy to see what it's
doing.

function global:MyimportCSV{
param($NoHeaderFile)

# Finds the number of columns in the file with no headers
$line = get-content $NoHeaderFile -totalCount 1
$array = $line.Split(',')

# Constructs a header line of the right length
$HeaderLine = ""
for($i = 0; $i -lt ($array.length)-1; $i++){
$HeaderLine += "Header($i),"
}
$HeaderLine += "Last Header"

# Gets the content of the file with no headers
$array = get-content $NoHeaderFile

# Outputs the header line to Temp.csv
set-content Temp.csv $HeaderLine

# Adds the content of the file with no headers to Temp.csv
foreach ($element in $array){
add-content Temp.csv $element
}

# Imports CSV as if the file with no headers had headers.
import-csv Temp.csv

}

I am sure that there are other ways of approaching this but the
preceding code does work for me.

Andrew Watt MVP
Old 11-14-2006   #6 (permalink)
Lee Holmes [MSFT]
Guest


 

Re: ::Split help

If you use Parse-TextObject, this becomes swimmingly easy:

gc test.csv | parse-textobject -delimiter ',' | select Property1

http://www.leeholmes.com/blog/parset...Vengeance.aspx


--
Lee Holmes [MSFT]
Windows PowerShell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

"Andrew Watt [MVP]" <SVGDeveloper@aol.com> wrote in message
news:0msbl2pe6fujkkv4ams3bv9herfepqbhm1@4ax.com...
> On 10 Nov 2006 09:41:51 -0800, mgrogan@gia.edu wrote:
>
>>I truly wish import-csv could handle csv files without headers. Maybe
>>it could come up with default field names or something...

>
> Hi,
>
> This function should do what you need, with a little adaptation
> perhaps.
>
> I have put comments in it so it should be fairly easy to see what it's
> doing.
>
> function global:MyimportCSV{
> param($NoHeaderFile)
>
> # Finds the number of columns in the file with no headers
> $line = get-content $NoHeaderFile -totalCount 1
> $array = $line.Split(',')
>
> # Constructs a header line of the right length
> $HeaderLine = ""
> for($i = 0; $i -lt ($array.length)-1; $i++){
> $HeaderLine += "Header($i),"
> }
> $HeaderLine += "Last Header"
>
> # Gets the content of the file with no headers
> $array = get-content $NoHeaderFile
>
> # Outputs the header line to Temp.csv
> set-content Temp.csv $HeaderLine
>
> # Adds the content of the file with no headers to Temp.csv
> foreach ($element in $array){
> add-content Temp.csv $element
> }
>
> # Imports CSV as if the file with no headers had headers.
> import-csv Temp.csv
>
> }
>
> I am sure that there are other ways of approaching this but the
> preceding code does work for me.
>
> Andrew Watt MVP



Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
regex help tonyr PowerShell 4 07-02-2008 02:13 PM
strange behavior from Regex.Split & myString.IndexOf mad.scientist.jr .NET General 4 04-04-2008 02:06 AM
Regex Help Christopher Robin .NET General 1 03-31-2008 02:02 PM
[Regex]'s split method Jobbsy PowerShell 17 11-26-2007 06:57 PM
Where are things like -replace and split([regex} documented? =?Utf-8?B?RXJzdHdoaWxlSUlJ?= PowerShell 2 07-25-2006 10:59 AM








Vistax64.com 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 2005-2008

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