【问题标题】:Delete empty rows in multiple csv files by using Powershell [duplicate]使用 Powershell 删除多个 csv 文件中的空行 [重复]
【发布时间】:2021-10-05 16:02:54
【问题描述】:

使用 Powershell 删除多个 CSV 文件中的空行。这是我为每个循环尝试的代码。但它正在写空文件。生成的文件应该以正确的格式在 excel 和 notepad++ 中打开。

Get-ChildItem $paramDest -Filter *Test*.csv | ForEach-Object {
$content = Get-Content $_.FullName | Where { $_.Replace("","","").trim() -ne "" }
Out-File -InputObject $content -FilePath $_.FullName
}

我还有一些其他文件集,代码应该适用于这两种文件。我可以为这 2 个单独的文件设置 2 个单独的代码。

这里是另一种文件示例格式

【问题讨论】:

标签: powershell powershell-2.0 powershell-3.0


【解决方案1】:

试试这个

输入

命令

Import-Csv -Path "C:\sample.csv" | Where-Object { $_.PSObject.Properties.Value -ne '' } | Export-Csv -Path "C:\Sample_clean.csv" -NoTypeInformation

输出*

编辑:更新多个文件,感谢@Santiago Squarzon

Get-ChildItem $paramDest -Filter *Test*.csv | 
Foreach-Object { 

# get the content of file
$content = Get-Content $_.FullName 

# replace all "," with empty string and exclude those lines and save the output to original file
$content  | where { $_.Replace('","','').trim('"') -ne '' } | Set-Content $_.FullName 
}

【讨论】:

  • 我有多个文件,尝试了下面的代码,没有按预期工作 $paramDest = "C:\Test\" Get-ChildItem $paramDest -Filter *.csv | ForEach-Object { Import-Csv -Path $_.FullName | Where-Object { $_.PSObject.Properties.Value -ne '' } | Export-Csv -Path $_.FullName -NoTypeInformation }
  • 你想为多个文件运行这一行吗?此示例适用于文件,并且可以完美运行
  • @user10260125 更新了多文件解决方案
猜你喜欢
  • 2021-09-29
  • 2021-09-27
  • 2023-03-27
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多