在PowerShell中为合并文件选择列



我使用get-childitem递归遍历目录(跳过顶层的一些),打开一系列csv文件,将文件名附加到每行数据的末尾并将数据合并为一个。

$mergedData= Get-ChildItem $path -Exclude yesterday,"OHCC Extract",output | 
Get-ChildItem  -recurse -Filter *csv  | 
Where-Object { $_.CreationTime -gt (Get-Date).AddDays(-1) } | 
% {
$file = $_.Name
$fn = $_.FullName
## capture the header line 
$FirstLine = Get-Content  $fn -TotalCount 1 
## add the column header for filename
$header = $FirstLine + ",Filename"
## get the contents of the files without the first line
Get-Content $fn  | SELECT -Skip 1   | %{ "$_,$file" }
}

现在每个文件有5列,ID,名,姓,电话,地址。列名用双引号括起来("ID", "First Name").现在的请求是跳过除ID和Last Name列之外的所有内容。所以我尝试了(从ID开始,稍后会添加First Name)

Get-Content $fn  | SELECT -Skip 1 -Property ID  | %{ "$_,$file" }

我在结果文件中得到@{ID=}。

Then I try

Get-Content $fn  | SELECT -Skip 1   | %{ $_.ID }

产生空白,然后

Import-Csv -Path  $fn -Delimiter ',' | SELECT ID 

给出@{ID=73aec2fe-6cb3-492e-a157-25e355ed9691}在这一点上,我只是在挣扎,因为我显然不知道如何处理PS中的对象。我在windows 10上安装了PowerShell 5.1.19041.1682。由于

我被要求提供样本数据,所以这里是。在多个子目录

中有35个文件输入FileA

<表类>column1IDColumn3tbody><<tr>东12苹果西方5梨

最新更新