1

The total number and position of fields vary from file to file, but the fields in which I am interested are always present. File1.csv looks like this:

1,1.0,one
2,2.0,two

See code below:

function Read_Headers_From_File($filename)
{
    # Yes, I am not reading a file right now
    "int,float,string"
}

$header1 = Read_Headers_From_File("file1.ini")
$file1 = Import-Csv -Header $header1 file1.csv
# $header2 = Read_Headers_From_File("file2.ini")
# $file2 = Import-Csv -Header $header2 file2.csv
echo $file1

The output is:

int,float,string
----------------
1
2

The "int,float,string" is interpreted as a single field named "int,float,string". Is there a way to make -Header "field1,field2,field3" work like -Header field1,field2,field3?

1
  • Suggest you edit this question to better explain the problem and the desired results. Commented Mar 12, 2016 at 1:36

2 Answers 2

2

You can do this to exclude a column, say y:

$userdata = Import-Csv test.csv | select-object * -exclude y

Although you didn't ask you can do this to rename columns:

# Assume you want to use the header names x,y,z instead of what 
# is specified in the header line of the file
# The Header parameter allows you to specify headers for a CSV that (nominally)
# doesn't contain a header line. But if the CSV file does contain a header line
# and -Header is specified then the header line is treated like a data line.
# The select -skip 1 discards the header-line-as-data row

$userdata = Import-Csv -Header x,y,z test.csv | select-object -skip 1
Sign up to request clarification or add additional context in comments.

4 Comments

Good to know, but my issue is that the names of the columns are fixed but the positions are variable, so I wanted to dynamically create headers to match the known positions read from a file. I suppose that I could just copy the file to a new file with dynamically-created headers included and then call Import-Csv.
You could just rename the columns. If the columns are labeled x,y,z, but are really z,x,y, then rename the columns to be z,x,y as the second part of the answer explains.
Remember my source CSV file does not have headers to be renamed. It seems that I can't add headers on the fly with -Headers, so the solution is to create a new file dest.csv with the headers that I want and populate it with the contents of source.csv.
The OP doesn't say your CSV doesn't have headers, so no I don't remember that. -Header (no "s") is exactly for specifying headers on the fly. Please edit the OP to make your question and desired results more clear.
0

I edited the question radically. What I want can be achieved by this:

$header1 = Read_Headers_From_File("file1.ini")
$file1 = Invoke-Expression ("Import-Csv " + "-Header " + $header1 + " file1.csv")

This also works:

# The ini file has a single row
# field1,field2,field3,...
function Read_Headers_From_File($ini_file)
{   
    $temp = Get-Content $ini_file
    # return array of args for -Header 
    $temp.Split(",")
}

$header1 = Read_Headers_From_File("file1.ini")
$file1 = Import-Csv file1.csv -Header $header1
echo $file1

Sorry for the noise. If possible please delete the question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.