Show Column Content in CSV file

Welcome Forums General PowerShell Q&A Show Column Content in CSV file

This topic contains 7 replies, has 3 voices, and was last updated by

 
Keymaster
1 year, 11 months ago.

  • Author
    Posts
  • #67990

    Participant
    Points: 25
    Rank: Member

    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

    Keymaster
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    $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

      Participant
      Points: 25
      Rank: Member

      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

      Keymaster
      Points: 1,811
      Helping HandTeam Member
      Rank: Community Hero

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

    • #68023

      Participant
      Points: 25
      Rank: Member

      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

    Participant
    Points: 240
    Helping Hand
    Rank: 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

      Participant
      Points: 25
      Rank: Member

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

  • #68028

    Keymaster
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    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.

The topic ‘Show Column Content in CSV file’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort