gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:31:59 26 4
gpt4 key购买 nike

我正在尝试遍历路径中的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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com