Sami
Sami

Reputation: 19

Adding Extra Headers in CSV

Input CSV:

CHeader1,CHeader2,CHeader3,CHeader4,CHeader5
a1,a2,a3,a4,a5
b1,b2,b3,b4,b5

Output CSV:

PHeader1,PHeader2
CHeader1,CHeader2,CHeader5
a1,a2,a5
b1,b2,b5

Already tried

Import-Csv -Path .\before.csv |
    select CHeader1, CHeader2, CHeader5 |
    Export-Csv -Path .\after.csv

This produces the file without parent level headers.

Any suggestions to add parent level headers in first line of CSV followed by client headers and then the data?

Upvotes: 0

Views: 69

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

What you're trying to create there is not actually a CSV, at least not in a way that the *-Csv cmdlets could handle. You can manually create it like this, though:

'PHeader1,PHeader2' | Set-Content '.\after.csv'
Import-Csv '.\before.csv' |
    Select-Object CHeader1, CHeader2, CHeader5 |
    ConvertTo-Csv -NoType |
    Add-Content '.\after.csv'

Upvotes: 2

Related Questions