Windows Vista Forums

Using $_.split then retrieving multiple values
  1. #1


    Marco Shaw Guest

    Using $_.split then retrieving multiple values

    I want to take a CSV file, and do a split, but then I also want to print out
    the 1st and 2nd columns.

    Something like:
    gc tmp.csv|foreach{$_.split(',')[0] [1]} (which doesn't seem to work like
    I'd want it to)
    User1 50
    User3 100
    User1 20



    The raw data:
    PS C:\> gc tmp.csv
    User1,50
    User3,100
    User1,20
    PS C:\> gc tmp.csv|foreach{$_.split(',')[0]}
    User1
    User3
    User1
    PS C:\> gc tmp.csv|foreach{$_.split(',')[1]}
    50
    100
    20

    Tried different combos to try to get both values together but the err out...

    Any ideas?



      My System SpecsSystem Spec

  2. #2


    Bruce Payette [MSFT] Guest

    Re: Using $_.split then retrieving multiple values

    On one-dimensional arrays, an array of indexes extracts multiple members.

    gc tmp.csv | foreach { $_.split()[0,1] }
    gc tmp.csv | foreach { $_.split()[3..5] }

    $indexes = 1,3,5,7
    gc tmp.csv | foreach { $_.split()[$indexes] }

    This also works for hashtables:

    PS (10) > @{a=1;b=2;c=3;d=4}["a","c"]
    1
    3

    On n-dimensional arrays, you can also do this but the notation is more
    complex. If you have an 2-d array in $data, then you could do

    $data[(0,0),(1,1),(2,2)]

    Composed indexing operations are for dealing with jagged arrays (arrays of
    arrays). Here's an example:

    PS (11) > $z = (1,2,3),(10,20,30),(100,200,300) # array of 3 elements,
    each of which is an array of 3 elements
    PS (12) > $z[1] # 2nd element
    10
    20
    30
    PS (13) > $z[1][1] # second element of the second element
    20
    PS (14) > $z[1][0] # first element of the second element
    10
    PS (15) > $z[1][2,3]
    30
    PS (16) > $z[1,2][0][2] # pathelogical - third element of the first element
    of a slice of the second and third elements
    30

    - bruce

    --
    Bruce Payette [MSFT]
    Windows PowerShell Technical Lead
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, and confers no rights.


    "Marco Shaw" <marco@Znbnet.nb.ca> wrote in message
    news:OeK7pgqCHHA.4772@TK2MSFTNGP02.phx.gbl...
    >I want to take a CSV file, and do a split, but then I also want to print
    >out the 1st and 2nd columns.
    >
    > Something like:
    > gc tmp.csv|foreach{$_.split(',')[0] [1]} (which doesn't seem to work like
    > I'd want it to)
    > User1 50
    > User3 100
    > User1 20
    >
    > The raw data:
    > PS C:\> gc tmp.csv
    > User1,50
    > User3,100
    > User1,20
    > PS C:\> gc tmp.csv|foreach{$_.split(',')[0]}
    > User1
    > User3
    > User1
    > PS C:\> gc tmp.csv|foreach{$_.split(',')[1]}
    > 50
    > 100
    > 20
    >
    > Tried different combos to try to get both values together but the err
    > out...
    >
    > Any ideas?
    >




      My System SpecsSystem Spec

  3. #3


    Marco Shaw Guest

    Re: Using $_.split then retrieving multiple values


    "Bruce Payette [MSFT]" <brucepay@microsoft.com> wrote in message
    news:Ow9IXwqCHHA.3540@TK2MSFTNGP03.phx.gbl...
    > On one-dimensional arrays, an array of indexes extracts multiple members.
    >
    > gc tmp.csv | foreach { $_.split()[0,1] }
    > gc tmp.csv | foreach { $_.split()[3..5] }


    Ok, right. I did try this. I should have been more specific in my original
    post.

    Won't the above put the entries one-per-line?

    PS C:\> gc tmp.csv|foreach{$_.split(',')[0,1]}
    User1
    50
    User3
    100
    User1
    20

    I actually want them showing as:
    User1 50
    User3 100
    User1 20

    I guess I could probably take each line as an array, split, then piece back
    together...

    Marco



      My System SpecsSystem Spec

  4. #4


    Adam Milazzo Guest

    Re: Using $_.split then retrieving multiple values

    Marco Shaw wrote:
    > "Bruce Payette [MSFT]" <brucepay@microsoft.com> wrote in message
    > news:Ow9IXwqCHHA.3540@TK2MSFTNGP03.phx.gbl...
    >> On one-dimensional arrays, an array of indexes extracts multiple members.
    >>
    >> gc tmp.csv | foreach { $_.split()[0,1] }
    >> gc tmp.csv | foreach { $_.split()[3..5] }

    >
    > Ok, right. I did try this. I should have been more specific in my original
    > post.
    >
    > Won't the above put the entries one-per-line?

    Because PowerShell collapses or unwraps arrays...

    So the foreach is returning 3 arrays:
    'User1', '50'
    'User3', '100'
    'User1', '20'

    And PowerShell is collapsing all of these into a single output stream:
    'User1', '50', 'User3', '100', 'User1', '20'

    Which it's then printing. But even if it didn't do that, it prints array
    elements on their own lines, so 3 arrays of 2 items, or 1 array of 6
    items would print the same way.

    You could piece them back together, or you could use the write-host
    command if you just want them output like that...

    Or you could use import-csv to create objects from your CSV file,
    assuming it has headers.

    Or you could create objects from them:

    PS> gc tmp.csv | foreach {
    $o = 1 | select User,Count
    $o.User,$o.Count = $_.split(',')[0,1]
    $o
    }

    User Count
    ---- -----
    User 1 50
    User 3 100
    User 1 20


    PS> gc tmp.csv|foreach{$_.split(',')[0,1]}

    > User1
    > 50
    > User3
    > 100
    > User1
    > 20
    >
    > I actually want them showing as:
    > User1 50
    > User3 100
    > User1 20
    >
    > I guess I could probably take each line as an array, split, then piece back
    > together...
    >
    > Marco
    >
    >


      My System SpecsSystem Spec

  5. #5


    Keith Hill [MVP] Guest

    Re: Using $_.split then retrieving multiple values

    "Marco Shaw" <marco@Znbnet.nb.ca> wrote in message
    news:e6P4T9qCHHA.4680@TK2MSFTNGP04.phx.gbl...
    > I actually want them showing as:
    > User1 50
    > User3 100
    > User1 20
    >
    > I guess I could probably take each line as an array, split, then piece
    > back together...


    Try this:

    35# gc tmp.csv | %{$arr=$_.Split(",");"{0} {1}" -f $arr[0], $arr[1]}
    User1 50
    User3 100
    User1 20

    "<format string>" -f arg1, arg2, arg<n> is the PowerShell way of using
    System.String.Format(formatString, arg1, arg2, etc).

    --
    Keith



      My System SpecsSystem Spec

  6. #6


    Maximilian Hänel Guest

    Re: Using $_.split then retrieving multiple values

    Hi Keith

    > Try this:
    >
    > 35# gc tmp.csv | %{$arr=$_.Split(",");"{0} {1}" -f $arr[0], $arr[1]}


    Or make it a little shorter:

    gc tmp.csv | %{"{0} {1}" -f $_.Split(",")}

    cu

    Max

      My System SpecsSystem Spec

  7. #7


    Jacques Barathon [MS] Guest

    Re: Using $_.split then retrieving multiple values

    "Maximilian Hänel" <ngSpam@smjh.de> wrote in message
    news:%23pSkLurCHHA.996@TK2MSFTNGP02.phx.gbl...
    > Or make it a little shorter:
    >
    > gc tmp.csv | %{"{0} {1}" -f $_.Split(",")}


    Or since the default output field separator is the space, you can write
    this:

    PS> gc temp.csv | % {"$($_.split(','))"}
    User1 50
    User3 100
    User1 20

    Jacques


      My System SpecsSystem Spec

Using $_.split then retrieving multiple values problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieving Property Values without Loading Referenced Assemblies dotNetDave .NET General 0 12 Apr 2010
Script to Export Multiple registry values? Ryan VB Script 2 11 Jul 2009
conditional formatting for multiple values sixschultzz .NET General 0 19 Sep 2008
Need help to split a string into text and numeric values !! karsagarwal VB Script 10 04 Sep 2008
Export-Csv multiple hashtable values PShelp PowerShell 2 10 Apr 2008