Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a column heading in a CSV file?

Tags:

powershell

csv

I cannot work out how to rename a column heading in a CSV file with Windows PowerShell. I can add new rows, delete rows, fill, and export, but cannot find out how to change a header value.

I thought I could import the CSV & export with headers specified, but taht doesn't work:

import-csv c:\tmp\test.csv | Select-Object first_name | Export-Csv -header "test" c:\tmp\test1.csv

I basically need to re-format a CSV file, so if I can select the data I want & specify the new headers that would be perfect.

like image 496
Paul Avatar asked Jan 10 '14 10:01

Paul


People also ask

How do I rename column headers?

Select a column, and then select Transform > Rename. You can also double-click the column header. Enter the new name.

How do I rename column names?

1. Renaming a column name using the ALTER keyword. Line 2: RENAME COLUMN OldColumnName TO NewColumnName; For Example: Write a query to rename the column name “SID” to “StudentsID”.


2 Answers

You may use Select-Object with a calculated property to do this:

Import-Csv test.csv |
    Select-Object @{ expression={$_.first_name}; label='test' } |
        Export-Csv -NoTypeInformation test1.csv
like image 189
jscott Avatar answered Oct 27 '22 02:10

jscott


Can't you try something like this?

import-csv 'c:\tmp\test.csv'  | select -Property @{name="test";expression={$($_.first_name)}}| Export-Csv c:\tmp\test1.csv
like image 30
JPBlanc Avatar answered Oct 27 '22 02:10

JPBlanc