Show Column Content in CSV file

Tagged: 

This topic contains 7 replies, has 3 voices, and was last updated by  Don Jones 4 months, 2 weeks ago.

  • Author
    Posts
  • #67990

    Ted Chen
    Participant

    I am trying to display the contents of column 1 and 2 in a CSV file. When I used the code below,

    $csv = Import-Csv C:transfer.csv
    $csv | Export-Csv transfer2.csv -NoTypeInformation
    $csv | Select-Object col1, col2 ## This line returns the header col1, col2 but there is no data.

    Transfer2.csv does contain data from transfer1.scv in column 1 & 2. But if I use Select-Object col1, col2, column1 and 2 come back completely empty. There was no error message. What am I doing wrong? What do I need to do to show contents of column 1 and 2 from transfer.csv?

  • #67992

    Don Jones
    Keymaster

    $csv contains the original transfer.csv. You exported that to transfer2.csv, but that did not change the contents of the $csv variable.

    What does the header of transfer.csv look like?

    • #68019

      Ted Chen
      Participant

      Thanks for the feedback. I am simply trying to display the contents of column1 and column 2 of transfer.csv to see if the data is read correctly. Ultimately, what I am trying to do is use column 1 and 2 of a CSV file as the input variables to a function which will write the result to column3. But I can't even seem to get the contents of column1 and 2 and I am not sure what I am doing wrong.

    • #68020

      Don Jones
      Keymaster

      Again, what's the header of the CSV look like?

    • #68023

      Ted Chen
      Participant

      transfer.csv has three columns and this is what it looks like:

      Source Destination Status
      —— ———– ——
      C:\file1.txt C:\Users\thchen\Desktop\ASPCTest\
      C:\file2.txt C:\Users\thchen\Desktop\ASPCTest\
      C:\file3.txt C:\Users\thchen\Desktop\ASPCTest\
      C:\file4.txt C:\Users\thchen\Desktop\ASPCTest\

      So the header of the three columns are 'Source', 'Destination' and 'Status'.

  • #68016

    Sam Boutros
    Participant

    $csv | Select-Object col1, col2 returns nothing probably because the column headers are not called col1, col2
    The code

    ($Csv | Get-Member | ? { $_.MemberType -eq 'NoteProperty' } | select -First 2).Name
    

    returns the names of the first 2 columns.

    Now try this:

    $csv = Import-Csv C:transfer.csv
    $csv | Export-Csv transfer2.csv -NoTypeInformation
    $Csv | select ($Csv | Get-Member | ? { $_.MemberType -eq 'NoteProperty' } | select -First 2).Name | FT -a 
    
    • #68025

      Ted Chen
      Participant

      Thank you both. I use "$csv | Select-Object Source, Destination, Status" and now it works!

  • #68028

    Don Jones
    Keymaster

    Yeah, so I think that's the point of confusion.

    When dealing with a CSV, Select-Object treats the COLUMN HEADERS as properties. You you could Select Source, Destination, and Status, because those are your column headers. But "Col1" isn't a thing. That's why – as noted above – you're getting blanks. Select-Object treats non-existent properties as new, blank properties, and outputs them.

You must be logged in to reply to this topic.