Select the CSV columns in Powershell where the header name contains a specific row

I have a data file of about 10-15 columns from which I want to extract specific columns. Some of the columns that I know are the exact column headings and others, I only know that the first two letters will always be “FC”. How to select only those columns where I know the column heading, and those starting with "FC"? Starting with the “FC” columns, I tried like this:

$myCSV = Import-CSV "mydata.txt" -Delimiter "`t"
$FCcols = $myCSV[0].psobject.Properties | foreach { $_.Name } | Where {$_ -match "FC"}
$myCSV | select $FCcols

But I just get the error message:

Select-Object : Cannot convert System.Management.Automation.PSObject to one of 
the following types {System.String, System.Management.Automation.ScriptBlock}.
At line:3 char:16
+ $myCSV | select <<<<  $FCcols
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], NotSupport 
   edException
    + FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Co 
   mmands.SelectObjectCommand

Then if I try:

$myCSV = Import-CSV "mydata.txt" -Delimiter "`t"
$FCcols = [System.Collections.ArrayList]@()
$myCSV[0].psobject.Properties | foreach { $_.Name } | Where {$_ -match "FC"} | %{$FCcols.Add($_)}
$myCSV | select $FCcols

I get the output that I want, except that it is in the format "header header: value", for example:

FC1839 : 0
FC1842 : 1
FC1843 : 6
FC1844 : 12
FC1845 : 4

FC1839 : 0
FC1842 : 0
FC1843 : 19
FC1844 : 22
FC1845 : 14

, - , , , .txt ( header: value)?

+4
4

: . , ( PSv2):

$myCSV | Select-Object FC*

() -Property , FC* ( ), FC.

, : 5 , PowerShell Format-List, .

, Format-Table ( , PowerShell , 4 ):

$myCSV | Select-Object FC* | Format-Table

CSV (TSV):

Import-Csv mydata.txt -Delimiter "`t" | Select-Object FC* | 
  Export-Csv myresults.txt -Encoding Utf8 -Delimiter "`t" -NoTypeInformation

:

Import-Csv mydata.txt -Delimiter "`t" | Select-Object FC* | 
  ConvertTo-Csv -Delimiter "`t" -NoTypeInformation | Select-Object -Skip 1 |
    Set-Content myresults.txt -Encoding Utf8

:

PSv2, .

([string[]]):

[string[]] $FCcols = $myCSV[0].psobject.Properties | % { $_.Name } | ? { $_ -match '^FC' }

, % ForEach-Object ? Where-Object.
, , -match, ^FC, , FC.


PSv3 +, :

$FCcols = $myCSV[0].psobject.Properties.Name -match "^FC"

, .Name .psobject.Properties, v3 + .Name , .

+3

Get-Member , :

$myCSV = Import-CSV "mydata.txt" -Delimiter "`t"
$myCSV | select ($myCSV | gm -MemberType NoteProperty | ? {$_.Name -match 'FC'}).Name
+1

Mathias - ; - , .

$myCSV | Select *FC*,ColumnIKnowTheNameOf

, Export-Csv, . , , Get-Member NoteProperty, csv/ .

$myCSV = Import-CSV "mydata.txt" -Delimiter "`t"

# you can get the headings by using Get-Member and Selecting NoteProperty members.
$FCcols = $myCSV |
            Get-Member |
            Where-Object {$_.MemberType -eq "NoteProperty" -and $_.Name -match "FC"} |
            Select-Object -ExpandProperty Name

# you add names to this array.
$FCcols += "ColumnIKnowTheNameOf"

$myCSV | Select-Object $FCcols

# to get a tab-delimited file similar to the one you imported, use Export-Csv
$myCSV | Export-csv "myresults.txt" -Delimiter "`t" -NoTypeInformation
0

- " " , , .

$myCSV = Import-CSV "mydata.txt" -Delimiter "`t" | select FC*
for ($i = 0; $i -lt $myCSV.count; $i++){
$writeline = ($myCSV[$i] | %{$_.PSObject.Properties | %{$_.Value}}) -join "`t"
ac "myresults.txt" $writeline -Encoding utf8}

The first row gives me the columns that I want, then the for loop gets the property values ​​of each column and concatenates them as table rows, and finally, each row is added to the text file. This may not be the pedagogically correct way to achieve a result, but it still works. Thanks to everyone for their contribution!

0
source

Source: https://habr.com/ru/post/1682320/


All Articles