Get a variable by dynamic variable name
up vote
1
down vote
favorite
How does one access data imported from a CSV file by using dynamic note property names? That is, one doesn't know the colunm names beforehand. They do match a pattern and are extracted from the CSV file when the script runs.
As for an example, consider a CSV file:
"Header 1","Header A","Header 3","Header B"
0,0,0,0
1,2,3,4
5,6,7,8
I'd like to extract only columns that end with a letter. To do this, I read the header row and extract names with a regex like so,
$reader = new-object IO.StreamReader("C:tmpdata.csv")
$line = $reader.ReadLine()
$headers = @()
$line.Split(",") | % {
$m = [regex]::match($_, '("Header [A-Z]")')
if($m.Success) { $headers += $m.value } }
This will get all the column names I care about:
"Header A"
"Header B"
Now, to access a CSV file I import it like so,
$csvData = import-csv "C:tmpdata.csv"
Import-CSV will create a custom object that has properties as per the header row. One can access the fields by NoteProperty names like so,
$csvData | % { $_."Header A" } # Works fine
This obviously requires one to know the column name in advance. I'd like to use colunn names I extracted and stored into the $headers
. How would I do that?
Some things I've tried so far
$csvData | % { $_.$headers[0] } # Error: Cannot index into a null array.
$csvData | % { $np = $headers[0]; $_.$np } # Doesn't print anything.
$csvData | % { $_.$($headers[0]) } # Doesn't print anything.
I could change the script like so it will write another a script that does know the column names. Is that my only solution?
powershell parameters
add a comment |
up vote
1
down vote
favorite
How does one access data imported from a CSV file by using dynamic note property names? That is, one doesn't know the colunm names beforehand. They do match a pattern and are extracted from the CSV file when the script runs.
As for an example, consider a CSV file:
"Header 1","Header A","Header 3","Header B"
0,0,0,0
1,2,3,4
5,6,7,8
I'd like to extract only columns that end with a letter. To do this, I read the header row and extract names with a regex like so,
$reader = new-object IO.StreamReader("C:tmpdata.csv")
$line = $reader.ReadLine()
$headers = @()
$line.Split(",") | % {
$m = [regex]::match($_, '("Header [A-Z]")')
if($m.Success) { $headers += $m.value } }
This will get all the column names I care about:
"Header A"
"Header B"
Now, to access a CSV file I import it like so,
$csvData = import-csv "C:tmpdata.csv"
Import-CSV will create a custom object that has properties as per the header row. One can access the fields by NoteProperty names like so,
$csvData | % { $_."Header A" } # Works fine
This obviously requires one to know the column name in advance. I'd like to use colunn names I extracted and stored into the $headers
. How would I do that?
Some things I've tried so far
$csvData | % { $_.$headers[0] } # Error: Cannot index into a null array.
$csvData | % { $np = $headers[0]; $_.$np } # Doesn't print anything.
$csvData | % { $_.$($headers[0]) } # Doesn't print anything.
I could change the script like so it will write another a script that does know the column names. Is that my only solution?
powershell parameters
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
How does one access data imported from a CSV file by using dynamic note property names? That is, one doesn't know the colunm names beforehand. They do match a pattern and are extracted from the CSV file when the script runs.
As for an example, consider a CSV file:
"Header 1","Header A","Header 3","Header B"
0,0,0,0
1,2,3,4
5,6,7,8
I'd like to extract only columns that end with a letter. To do this, I read the header row and extract names with a regex like so,
$reader = new-object IO.StreamReader("C:tmpdata.csv")
$line = $reader.ReadLine()
$headers = @()
$line.Split(",") | % {
$m = [regex]::match($_, '("Header [A-Z]")')
if($m.Success) { $headers += $m.value } }
This will get all the column names I care about:
"Header A"
"Header B"
Now, to access a CSV file I import it like so,
$csvData = import-csv "C:tmpdata.csv"
Import-CSV will create a custom object that has properties as per the header row. One can access the fields by NoteProperty names like so,
$csvData | % { $_."Header A" } # Works fine
This obviously requires one to know the column name in advance. I'd like to use colunn names I extracted and stored into the $headers
. How would I do that?
Some things I've tried so far
$csvData | % { $_.$headers[0] } # Error: Cannot index into a null array.
$csvData | % { $np = $headers[0]; $_.$np } # Doesn't print anything.
$csvData | % { $_.$($headers[0]) } # Doesn't print anything.
I could change the script like so it will write another a script that does know the column names. Is that my only solution?
powershell parameters
How does one access data imported from a CSV file by using dynamic note property names? That is, one doesn't know the colunm names beforehand. They do match a pattern and are extracted from the CSV file when the script runs.
As for an example, consider a CSV file:
"Header 1","Header A","Header 3","Header B"
0,0,0,0
1,2,3,4
5,6,7,8
I'd like to extract only columns that end with a letter. To do this, I read the header row and extract names with a regex like so,
$reader = new-object IO.StreamReader("C:tmpdata.csv")
$line = $reader.ReadLine()
$headers = @()
$line.Split(",") | % {
$m = [regex]::match($_, '("Header [A-Z]")')
if($m.Success) { $headers += $m.value } }
This will get all the column names I care about:
"Header A"
"Header B"
Now, to access a CSV file I import it like so,
$csvData = import-csv "C:tmpdata.csv"
Import-CSV will create a custom object that has properties as per the header row. One can access the fields by NoteProperty names like so,
$csvData | % { $_."Header A" } # Works fine
This obviously requires one to know the column name in advance. I'd like to use colunn names I extracted and stored into the $headers
. How would I do that?
Some things I've tried so far
$csvData | % { $_.$headers[0] } # Error: Cannot index into a null array.
$csvData | % { $np = $headers[0]; $_.$np } # Doesn't print anything.
$csvData | % { $_.$($headers[0]) } # Doesn't print anything.
I could change the script like so it will write another a script that does know the column names. Is that my only solution?
powershell parameters
powershell parameters
edited Nov 19 at 9:18
Ansgar Wiechers
138k12118181
138k12118181
asked Dec 15 '11 at 19:23
vonPryz
12.2k23342
12.2k23342
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
the first thing ( and the only one... sorry) that came in my mind is:
$csvData | % { $_.$(( $csvData | gm | ? { $_.membertype -eq "noteproperty"} )[0].name) }
for get the first's column values and
$csvData | % { $_.$(( $csvData | gm | ? { $_.membertype -eq "noteproperty"} )[1].name) }
for second column and so on....
is this what you need?
Almost. It requires one to know the indexes in advance, but can be fixed easily enough by adding a -like search:$csvData | % { $_.$(( $csvData | gm | ? { $_.membertype -eq "noteproperty" -and $_.Name -like $($headers[0])} ).name) }
– vonPryz
Dec 16 '11 at 5:48
Thanks, this was exactly what I needed.
– vonPryz
Dec 16 '11 at 17:09
@vonPryz - How is this exactly what you needed? Christian - No offence.
– manojlds
Dec 16 '11 at 23:30
add a comment |
up vote
3
down vote
I think you want this:
[string]$headers = $csvdata | gm -MemberType "noteproperty" |
?{ $_.Name -match "Header [a-zA-Z]$"} |
select -expand Name
$csvdata | select $headers
Choose the headers that match the condition (in this case, ones ending with characters) and then get the csv data for those headers.
add a comment |
up vote
0
down vote
you can use custom script to parse csv manually:
$content = Get-Content "C:tmpdata.csv"
$header = $content | Select -first 1
$columns = $header.Split(",")
$indexes = @()
for($i; $i -lt $columns.Count;$i++)
{
# to verify whether string end with letter matches this regex: "[A-Za-z]$"
if ($column[$i] -match "[A-Za-z]$")
{
$indexes += $i
}
}
$outputFile = "C:tmpoutdata.csv"
Remove-Item $outputFile -ea 0
foreach ($line in $content)
{
$output = ""
$rowcol = $line.Split(",")
[string]::Join(",", ($indexes | foreach { $rowcol[$_] })) | Add-Content $outputFile
}
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
the first thing ( and the only one... sorry) that came in my mind is:
$csvData | % { $_.$(( $csvData | gm | ? { $_.membertype -eq "noteproperty"} )[0].name) }
for get the first's column values and
$csvData | % { $_.$(( $csvData | gm | ? { $_.membertype -eq "noteproperty"} )[1].name) }
for second column and so on....
is this what you need?
Almost. It requires one to know the indexes in advance, but can be fixed easily enough by adding a -like search:$csvData | % { $_.$(( $csvData | gm | ? { $_.membertype -eq "noteproperty" -and $_.Name -like $($headers[0])} ).name) }
– vonPryz
Dec 16 '11 at 5:48
Thanks, this was exactly what I needed.
– vonPryz
Dec 16 '11 at 17:09
@vonPryz - How is this exactly what you needed? Christian - No offence.
– manojlds
Dec 16 '11 at 23:30
add a comment |
up vote
0
down vote
accepted
the first thing ( and the only one... sorry) that came in my mind is:
$csvData | % { $_.$(( $csvData | gm | ? { $_.membertype -eq "noteproperty"} )[0].name) }
for get the first's column values and
$csvData | % { $_.$(( $csvData | gm | ? { $_.membertype -eq "noteproperty"} )[1].name) }
for second column and so on....
is this what you need?
Almost. It requires one to know the indexes in advance, but can be fixed easily enough by adding a -like search:$csvData | % { $_.$(( $csvData | gm | ? { $_.membertype -eq "noteproperty" -and $_.Name -like $($headers[0])} ).name) }
– vonPryz
Dec 16 '11 at 5:48
Thanks, this was exactly what I needed.
– vonPryz
Dec 16 '11 at 17:09
@vonPryz - How is this exactly what you needed? Christian - No offence.
– manojlds
Dec 16 '11 at 23:30
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
the first thing ( and the only one... sorry) that came in my mind is:
$csvData | % { $_.$(( $csvData | gm | ? { $_.membertype -eq "noteproperty"} )[0].name) }
for get the first's column values and
$csvData | % { $_.$(( $csvData | gm | ? { $_.membertype -eq "noteproperty"} )[1].name) }
for second column and so on....
is this what you need?
the first thing ( and the only one... sorry) that came in my mind is:
$csvData | % { $_.$(( $csvData | gm | ? { $_.membertype -eq "noteproperty"} )[0].name) }
for get the first's column values and
$csvData | % { $_.$(( $csvData | gm | ? { $_.membertype -eq "noteproperty"} )[1].name) }
for second column and so on....
is this what you need?
answered Dec 15 '11 at 20:21
CB.
43.7k593114
43.7k593114
Almost. It requires one to know the indexes in advance, but can be fixed easily enough by adding a -like search:$csvData | % { $_.$(( $csvData | gm | ? { $_.membertype -eq "noteproperty" -and $_.Name -like $($headers[0])} ).name) }
– vonPryz
Dec 16 '11 at 5:48
Thanks, this was exactly what I needed.
– vonPryz
Dec 16 '11 at 17:09
@vonPryz - How is this exactly what you needed? Christian - No offence.
– manojlds
Dec 16 '11 at 23:30
add a comment |
Almost. It requires one to know the indexes in advance, but can be fixed easily enough by adding a -like search:$csvData | % { $_.$(( $csvData | gm | ? { $_.membertype -eq "noteproperty" -and $_.Name -like $($headers[0])} ).name) }
– vonPryz
Dec 16 '11 at 5:48
Thanks, this was exactly what I needed.
– vonPryz
Dec 16 '11 at 17:09
@vonPryz - How is this exactly what you needed? Christian - No offence.
– manojlds
Dec 16 '11 at 23:30
Almost. It requires one to know the indexes in advance, but can be fixed easily enough by adding a -like search:
$csvData | % { $_.$(( $csvData | gm | ? { $_.membertype -eq "noteproperty" -and $_.Name -like $($headers[0])} ).name) }
– vonPryz
Dec 16 '11 at 5:48
Almost. It requires one to know the indexes in advance, but can be fixed easily enough by adding a -like search:
$csvData | % { $_.$(( $csvData | gm | ? { $_.membertype -eq "noteproperty" -and $_.Name -like $($headers[0])} ).name) }
– vonPryz
Dec 16 '11 at 5:48
Thanks, this was exactly what I needed.
– vonPryz
Dec 16 '11 at 17:09
Thanks, this was exactly what I needed.
– vonPryz
Dec 16 '11 at 17:09
@vonPryz - How is this exactly what you needed? Christian - No offence.
– manojlds
Dec 16 '11 at 23:30
@vonPryz - How is this exactly what you needed? Christian - No offence.
– manojlds
Dec 16 '11 at 23:30
add a comment |
up vote
3
down vote
I think you want this:
[string]$headers = $csvdata | gm -MemberType "noteproperty" |
?{ $_.Name -match "Header [a-zA-Z]$"} |
select -expand Name
$csvdata | select $headers
Choose the headers that match the condition (in this case, ones ending with characters) and then get the csv data for those headers.
add a comment |
up vote
3
down vote
I think you want this:
[string]$headers = $csvdata | gm -MemberType "noteproperty" |
?{ $_.Name -match "Header [a-zA-Z]$"} |
select -expand Name
$csvdata | select $headers
Choose the headers that match the condition (in this case, ones ending with characters) and then get the csv data for those headers.
add a comment |
up vote
3
down vote
up vote
3
down vote
I think you want this:
[string]$headers = $csvdata | gm -MemberType "noteproperty" |
?{ $_.Name -match "Header [a-zA-Z]$"} |
select -expand Name
$csvdata | select $headers
Choose the headers that match the condition (in this case, ones ending with characters) and then get the csv data for those headers.
I think you want this:
[string]$headers = $csvdata | gm -MemberType "noteproperty" |
?{ $_.Name -match "Header [a-zA-Z]$"} |
select -expand Name
$csvdata | select $headers
Choose the headers that match the condition (in this case, ones ending with characters) and then get the csv data for those headers.
answered Dec 15 '11 at 20:50
manojlds
210k46368357
210k46368357
add a comment |
add a comment |
up vote
0
down vote
you can use custom script to parse csv manually:
$content = Get-Content "C:tmpdata.csv"
$header = $content | Select -first 1
$columns = $header.Split(",")
$indexes = @()
for($i; $i -lt $columns.Count;$i++)
{
# to verify whether string end with letter matches this regex: "[A-Za-z]$"
if ($column[$i] -match "[A-Za-z]$")
{
$indexes += $i
}
}
$outputFile = "C:tmpoutdata.csv"
Remove-Item $outputFile -ea 0
foreach ($line in $content)
{
$output = ""
$rowcol = $line.Split(",")
[string]::Join(",", ($indexes | foreach { $rowcol[$_] })) | Add-Content $outputFile
}
add a comment |
up vote
0
down vote
you can use custom script to parse csv manually:
$content = Get-Content "C:tmpdata.csv"
$header = $content | Select -first 1
$columns = $header.Split(",")
$indexes = @()
for($i; $i -lt $columns.Count;$i++)
{
# to verify whether string end with letter matches this regex: "[A-Za-z]$"
if ($column[$i] -match "[A-Za-z]$")
{
$indexes += $i
}
}
$outputFile = "C:tmpoutdata.csv"
Remove-Item $outputFile -ea 0
foreach ($line in $content)
{
$output = ""
$rowcol = $line.Split(",")
[string]::Join(",", ($indexes | foreach { $rowcol[$_] })) | Add-Content $outputFile
}
add a comment |
up vote
0
down vote
up vote
0
down vote
you can use custom script to parse csv manually:
$content = Get-Content "C:tmpdata.csv"
$header = $content | Select -first 1
$columns = $header.Split(",")
$indexes = @()
for($i; $i -lt $columns.Count;$i++)
{
# to verify whether string end with letter matches this regex: "[A-Za-z]$"
if ($column[$i] -match "[A-Za-z]$")
{
$indexes += $i
}
}
$outputFile = "C:tmpoutdata.csv"
Remove-Item $outputFile -ea 0
foreach ($line in $content)
{
$output = ""
$rowcol = $line.Split(",")
[string]::Join(",", ($indexes | foreach { $rowcol[$_] })) | Add-Content $outputFile
}
you can use custom script to parse csv manually:
$content = Get-Content "C:tmpdata.csv"
$header = $content | Select -first 1
$columns = $header.Split(",")
$indexes = @()
for($i; $i -lt $columns.Count;$i++)
{
# to verify whether string end with letter matches this regex: "[A-Za-z]$"
if ($column[$i] -match "[A-Za-z]$")
{
$indexes += $i
}
}
$outputFile = "C:tmpoutdata.csv"
Remove-Item $outputFile -ea 0
foreach ($line in $content)
{
$output = ""
$rowcol = $line.Split(",")
[string]::Join(",", ($indexes | foreach { $rowcol[$_] })) | Add-Content $outputFile
}
answered Dec 15 '11 at 20:51
Tomas Panik
2,7411828
2,7411828
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8525481%2fget-a-variable-by-dynamic-variable-name%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown