PowerShell-替换csv文件中特定列中的字符 [英] PowerShell - Replace a character in a specific column in a csv file
本文介绍了PowerShell-替换csv文件中特定列中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在许多csv文件中,除了Name
列(其中可能包含我需要保留的-
字符)之外,我需要将"-"
替换为""
(什么都没有).
I need to replace "-"
for ""
(nothing) in a many csv files, except for the Name
column, which may contain -
characters I need to keep.
例如:
"Name","timestamp","CPU|Demand (%)","CPU|Demand (%) (Trend)","CPU|Demand (%) (30 days forecast)"
"ZY02-LAB-WinMachine","Mar 2, 2017 12:01:19 AM","-","38.07","-"
"ZY02-LAB-WinMachine","Mar 21, 2017 10:45:00 AM","40.55","-","-"
"ZY02-LAB-WinMachine","Apr 6, 2017 11:56:19 AM","-","-","38.69"
"ZY02-LAB-WinMachine","Apr 6, 2017 12:11:19 PM","-","-","38.7"
将成为
"Name","timestamp","CPU|Demand (%)","CPU|Demand (%) (Trend)","CPU|Demand (%) (30 days forecast)"
"ZY02-LAB-WinMachine","Mar 2, 2017 12:01:19 AM","","38.07",""
"ZY02-LAB-WinMachine","Mar 21, 2017 10:45:00 AM","40.55","",""
"ZY02-LAB-WinMachine","Apr 6, 2017 11:56:19 AM","","","38.69"
"ZY02-LAB-WinMachine","Apr 6, 2017 12:11:19 PM","","","38.7"
我在脚本中包含的行替换了csv中的所有-
..甚至是Name
列:-(
The line I have in my script replaces ALL -
in the csv .. even the Name
column :-(
(Get-Content $ImportCPUFile) | % {$_ -replace "-"} | out-file -FilePath CSV-cleaned.csv -Fo -En ascii
推荐答案
尝试一下:
(Get-Content $ImportCPUFile) |
ForEach-Object { $_ -replace '"-"', '""' } |
Out-File -FilePath CSV-cleaned.csv -Force -Encoding ascii
这篇关于PowerShell-替换csv文件中特定列中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文