我有csv文件,其中包含以下样本的数据
MACADD||TEST|Street1|CITY||Taiwan|||10000000|||FIRE||Taiwan||||||||12天数||30 Days|DDTE||812148709231890||124-Taiwan|DENE|||
我想要替换每个csv文件中的第7个值,但问题是,当我替换它时,整个数据被逗号分隔,作为其中包含逗号的替换值。
有没有办法在替换发生后忽略/转义逗号,以便整个数据出现在最终csv文件的第一个单元格中。
$Files = Get-content -Path F:\Newfolder\*.csv
$CountryCodeLookup = @{
'USA'= 'United States'
'Taiwan' = "Taiwan, Republic of China" # for this item csv file become strange
'Delhi' = "Delhi, Capital of India, U.T" # for this item csv file become strange
}
foreach ($File in $Files)
{
$DelimCount = ($File -replace '[^|]', '').Length
$CSV_Thing = ConvertFrom-Csv -Delimiter '|' -InputObject $File -Header @(1..$DelimCount)
If ($CountryCodeLookup.ContainsKey($CSV_Thing.7))
{
$CSV_Thing.7 = $CountryCodeLookup[$CSV_Thing.7]
}
$OutString = (($CSV_Thing |
ConvertTo-Csv -Delimiter '|' -NoTypeInformation |
Select-Object -Skip 1) -replace '"', '') + '|' | Set-Content $File.PSPath -Force
}发布于 2020-05-28 21:09:18
$Files = Get-content -Path F:\Newfolder\*.txt
$Path = "F:\Newfolder"
$CountryCodeLookup = @{
'USA'= 'United States'
'Taiwan' = "Taiwan, Republic of China" # for this item csv file become strange
'Delhi' = "Delhi, Capital of India, U.T" # for this item csv file become strange
}
foreach ($File in $Files)
{
$DelimCount = ($File -replace '[^|]', '').Length
$CSV_Thing = ConvertFrom-Csv -Delimiter '|' -InputObject $File -Header @(1..$DelimCount)
If ($CountryCodeLookup.ContainsKey($CSV_Thing.7))
{
$CSV_Thing.7 = $CountryCodeLookup[$CSV_Thing.7]
}
$OutString = (($CSV_Thing |
ConvertTo-Csv -Delimiter '|' -NoTypeInformation |
Select-Object -Skip 1) -replace '"', '') + '|' | Set-Content $File.PSPath -Force
}
$files = Get-ChildItem $Path | where { ! $_.PSIsContainer }
foreach ($file in $files){
$newFileName = ($file.Fullname) -Replace ".txt",".csv"
Get-Content $file.FullName | Out-File $newFileName
}https://stackoverflow.com/questions/62045851
复制相似问题