Power shell 代码删除列 headers 中的特殊字符(.csv 文件)
Power shell code to remove special characters in the column headers(.csv file)
我正在尝试遍历路径中的 40 个 CSV 文件,并仅在 headers.
中删除所有非数字、字母和 space 值的字符
下面是我尝试处理的代码,这适用于文件中的 headers,但它也替换了文件中的所有数据,我只能看到 headers 中没有特殊字符它,我只是一个初学者 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
}
-replace 运算符需要两个值,第一个值是您要查找的值,第二个值是用什么替换第一个值。
示例:
"John Jones" -replace "Jones","Smith"
这将用文本 "Smith" 替换 "Jones" 创建一个新字符串 "John Smith"
在您的示例中,不是创建要保留内容的正则表达式,而是创建要替换内容的正则表达式。
示例:
$path = "C:\AllFiles\"
Get-ChildItem -path $path -Filter *.csv |
Foreach-Object {
$content = Get-Content -Path $path
$content[0] = $content[0] -replace '[regex for special chars]',""
Set-Content $path -value $content -force
}
这会将整个字符串替换为您已将正则表达式值替换为“”
的字符串
非常接近,试试这样:
$path = "C:\temp"
Get-ChildItem -path $path -Filter *.csv |
Foreach-Object {
$content = Get-Content $_
$content[0] = $content[0] -replace '[^a-zA-Z0-9, ]',''
$content | Out-File $_
}
这只会清除第一行中的特殊字符,而不会影响文件的其余部分。
这应该可以解决问题并且应该是最快的方法:
$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
}
试试这个:
dir "C:\AllFiles" -Filter *.csv | % {
(Get-Content $_.FullName)[0] -replace '[\W]', '' | Set-Content $_.FullName -Force
}
我正在尝试遍历路径中的 40 个 CSV 文件,并仅在 headers.
中删除所有非数字、字母和 space 值的字符下面是我尝试处理的代码,这适用于文件中的 headers,但它也替换了文件中的所有数据,我只能看到 headers 中没有特殊字符它,我只是一个初学者 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
}
-replace 运算符需要两个值,第一个值是您要查找的值,第二个值是用什么替换第一个值。
示例:
"John Jones" -replace "Jones","Smith"
这将用文本 "Smith" 替换 "Jones" 创建一个新字符串 "John Smith"
在您的示例中,不是创建要保留内容的正则表达式,而是创建要替换内容的正则表达式。
示例:
$path = "C:\AllFiles\"
Get-ChildItem -path $path -Filter *.csv |
Foreach-Object {
$content = Get-Content -Path $path
$content[0] = $content[0] -replace '[regex for special chars]',""
Set-Content $path -value $content -force
}
这会将整个字符串替换为您已将正则表达式值替换为“”
的字符串非常接近,试试这样:
$path = "C:\temp"
Get-ChildItem -path $path -Filter *.csv |
Foreach-Object {
$content = Get-Content $_
$content[0] = $content[0] -replace '[^a-zA-Z0-9, ]',''
$content | Out-File $_
}
这只会清除第一行中的特殊字符,而不会影响文件的其余部分。
这应该可以解决问题并且应该是最快的方法:
$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
}
试试这个:
dir "C:\AllFiles" -Filter *.csv | % {
(Get-Content $_.FullName)[0] -replace '[\W]', '' | Set-Content $_.FullName -Force
}