我试图从目录中的多个文件中删除最后2列。 这段代码接近我的需要,但它只是删除了最后一列(包括HEADER7)。它还会删除HEADER6,但第6列的所有数据都保持不变。 最后一列包含每个文件的文件名。 文件以逗号分隔。
$InputFolder = '.\'
$OutputFolder = '.\'
Get-ChildItem $InputFolder -Filter *.* |
where {-not $_.psiscontainer} |
foreach {
$FileName = $_.Name
$BaseName = $_.Basename
$data = Get-Content $_ -ReadCount 0
$($data[0]) -replace ",HEADER6",'' -replace ",HEADER7",'' | Set-Content $OutputFolder\$FileName
$data[1..($data.Length -1)] -replace ",$HEADER6",'' -replace ",$BaseName",'' | Add-Content $OutputFolder\$FileName
}
档案样本
HEADER1,HEADER2,HEADER3,HEADER4,HEADER5,HEADER6,HEADER7
22.1,34.02,12.00,23.44,123.33,12.34,FILE1
25.1,30.22,12.55,21.40,13.03,15.31,FILE1
15.51,10.24,11.54,11.50,12.13,17.14,FILE1
。 。
答案 0 :(得分:1)
您可以尝试字符串操作。如果您有大文件,它应该比import-/export-csv
快:
Get-ChildItem $InputFolder -Filter *.* |
Where-Object {-not $_.psiscontainer} |
ForEach-Object {
$FileName = $_.Name
$text = [System.IO.File]::ReadAllLines($_.FullName)
$modifiedtext = foreach($line in $text) {
$line -replace '(?<=\S*?,\S*?,\S*?,\S*?,\S*?),.*'
}
$modifiedtext | Set-Content $OutputFolder\$FileName
#The line below might be even faster.
#[System.IO.File]::WriteAllLines(((Resolve-Path $OutputFolder\$FileName).Path), $modifiedtext)
}
答案 1 :(得分:1)
看看这是否更快:
$InputFolder = '.\'
$OutputFolder = '.\'
Get-ChildItem $InputFolder -Filter *.* |
where {-not $_.psiscontainer} |
foreach {
$FileName = $_.Name
(Get-Content $_ -ReadCount 0 ) -replace ',[^,]+,[^,]+$' |
Set-Content $OutputFolder\$FileName
}
答案 2 :(得分:0)
您可以导入CSV,选择要保留的列并直接导出
Import-Csv c:\TEMP\test.csv | Select-Object -property HEADER1,HEADER2,HEADER3,HEADER4,HEADER5 | Export-Csv -NoTypeInformation c:\temp\test2.csv