啊,既然您已经引用了 CSV 值,这应该不会太难。
# I have faked the input using a here-string, but in real life, you should use
# $csv = Import-Csv -Path <PATH TO THE CSV FILE>
$csv = @"
Name,Department,Team,Task
"Jack","QA","AF","He need to work
He needs to update
He needs to plan"
"Sam","Dev","Retail","He need to work
He needs to update
He needs to plan"
"@ | ConvertFrom-Csv
# convert all newlines to a full-stop dot and replace multiple spaces in the Task field to become a single space character
$csv | ForEach-Object {
$_.Task = $_.Task -replace '[\r?\n]+', '. ' -replace '\s{2,}', ' '
# if you just want to 'normalize' whitespaces like a browser does, use this instead.
# $_.Task = $_.Task -replace '\s+', ' '
}
现在$csv 变量保存了这些数据:
名称 部门 团队任务
---- ---------- ---- ----
Jack QA AF 他需要工作。他需要更新。他需要计划
Sam Dev 零售 他需要工作。他需要更新。他需要计划
接下来,使用分号作为分隔符写入更新后的 CSV 文件
$csv | Export-Csv -Path '<PATH TO THE EXPORTED CSV FILE>' -NoTypeInformation -Delimiter ';'
希望有所帮助
编辑
根据您的评论,我了解更多列中可能包含换行符。
这是更新后的脚本,如果文件中包含换行符,它将把文件中的任何字段转换为单个字符串。
# Again, I have faked the input using a here-string, but in real life, you should use
# $csv = Import-Csv -Path <PATH TO THE CSV FILE>
$csv = @"
Name,Department,Team,Task
"Jack","QA","AF
XYZ","He need to work
He needs to update
He needs to plan"
"Sam","Dev","Retail
Sales","He need to work
He needs to update
He needs to plan"
"@ | ConvertFrom-Csv
# get an array of the header names
$headers = $csv[0].PSObject.Properties.name
$csv | ForEach-Object {
foreach ($hdr in $headers) {
# this regex converts all newlines to a full-stop dot and replaces multiple spaces to become one single space character
$_.$hdr = $_.$hdr -replace '[\r?\n]+', '. ' -replace '\s{2,}', ' '
# if you just want to 'normalize' all whitespaces like a browser does, use this instead.
# $_.$hdr = $_.$hdr -replace '\s+', ' '
}
}
之后,$csv 变量保存了这些数据:
Name Department Team Task
---- ---------- ---- ----
Jack QA AF. XYZ He need to work. He needs to update. He needs to plan
Sam Dev Retail. Sales He need to work. He needs to update. He needs to plan
像往常一样导出到新的 CSV 文件:
$csv | Export-Csv -Path '<PATH TO THE EXPORTED CSV FILE>' -NoTypeInformation -Delimiter ';'