powershell - Power Shell代码删除列标题中的特殊字符(.csv文件)

标签 powershell powershell-2.0 powershell-3.0

我正在尝试遍历路径中的40个CSV文件,并删除仅在标题中不是数字,字母和空格值的任何字符。

以下是我尝试处理的代码,它适用于文件中的 header ,但也可以替换文件中的所有数据,我只能看到 header 中没有特殊字符,我只是Power Shell的初学者,确保如何进一步进行任何帮助深表感谢。

$path = "C:\AllFiles\"

Get-ChildItem -path $path -Filter *.csv | 

Foreach-Object {

$content = Get-Content $_.FullName

$content[0] = $content[0] -replace '[^0-9a-zA-Z, ]'|Set-Content $_.FullName

}

最佳答案

这应该可以解决问题,并且应该是最快的方法:

$path = 'C:\AllFiles\'

$collection = Get-ChildItem -path $path -Filter *.csv' 

foreach( $file in $collection ) {

    $content = [System.IO.File]::ReadAllLines( $file.FullName )

    $content[0] = $content[0] -replace '[^0-9a-zA-Z, ]'

    [System.IO.File]::WriteAllLines( $file.FullName, $content ) | Out-Null 
}

关于powershell - Power Shell代码删除列标题中的特殊字符(.csv文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60191348/

相关文章:

powershell - 如何在 PowerShell 中查找 ArrayList 中的 indexOf 元素

powershell - PowerShell Set-Member 函数和 $_ 的用法

powershell - PowerShell 中的 `>`(重定向)和 Out-File 有什么区别

algorithm - PowerShell V2.0中计算SHA1哈希算法

windows - 为什么安装Chocolatey时PowerShell是 "not recognized"?

powershell - 计划 Powershell 更改 ObjectType

powershell - 括号中的变量无法识别

powershell - 使用System.IO.Compression.FileSystem将完整目录添加到现有zip文件中

powershell - 如何使用 ps1 脚本打开 image.png?

windows - 在 Windows 中,如何将已替换为十六进制 ASCII 的原始字符恢复到 csv 文件中?