Windows Vista Forums

[regex]::Split help
  1. #1


    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:\>



      My System SpecsSystem Spec

  2. #2


    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

      My System SpecsSystem Spec

  3. #3


    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



      My System SpecsSystem Spec

  4. #4


    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!





      My System SpecsSystem Spec

  5. #5


    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

      My System SpecsSystem Spec

  6. #6


    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




      My System SpecsSystem Spec

[regex]::Split help problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: CSV and regex s.split(",") and empty fields Dr J R Stockton VB Script 0 05 Sep 2009
Re: how to with regex split into tokens Kiron PowerShell 2 11 Dec 2008
strange behavior from Regex.Split & myString.IndexOf mad.scientist.jr .NET General 4 04 Apr 2008
[Regex]'s split method Jobbsy PowerShell 17 26 Nov 2007
Where are things like -replace and split([regex} documented? =?Utf-8?B?RXJzdHdoaWxlSUlJ?= PowerShell 2 25 Jul 2006