我使用ConvertTo-Csv
獲得逗號分隔的輸出的ConvertTo-CSV輸出不帶引號
get-process | convertto-csv -NoTypeInformation -Delimiter ","
它輸出,如:
"__NounName","Name","Handles","VM","WS",".....
不過,我想獲得輸出時沒有引號,像
__NounName,Name,Handles,VM,WS....
我使用ConvertTo-Csv
獲得逗號分隔的輸出的ConvertTo-CSV輸出不帶引號
get-process | convertto-csv -NoTypeInformation -Delimiter ","
它輸出,如:
"__NounName","Name","Handles","VM","WS",".....
不過,我想獲得輸出時沒有引號,像
__NounName,Name,Handles,VM,WS....
這裏有一個方法來刪除引號
get-process | convertto-csv -NoTypeInformation -Delimiter "," | % {$_ -replace '"',''}
但它有一個嚴重的缺點,如果該項目的一個包含"
將被刪除!
我沒有花太多時間尋找刪除報價。但是,這是一個解決方法。
get-process | Export-Csv -NoTypeInformation -Verbose -Path $env:temp\test.csv
$csv = Import-Csv -Path $env:temp\test.csv
這是一個快速的解決方法,可能有更好的方法來做到這一點。
這與接受的答案非常相似,但它有助於防止不必要地刪除「真實」引號。
$delimiter = ','
Get-Process | ConvertTo-Csv -Delimiter $delimiter -NoTypeInformation | foreach { $_ -replace '^"','' -replace "`"$delimiter`"",$delimiter -replace '"$','' }
這將做到以下幾點:
因此,如果其中一個值實際上不僅包含引號,而且還特別包含一個引號分隔符引用序列,該引用序列希望應該不常見。
我遇到了這個問題,發現了這個問題,但對答案並不滿意,因爲如果你使用的數據包含一個分隔符,它們應該保持引用,它們似乎都受到了影響。擺脫不需要的雙引號是一件好事。
下面的解決方案似乎解決了一般情況下的這個問題,以及所有會導致問題的變體。
我在其他地方找到了這個答案,Removing quotes from CSV created by PowerShell,並用它來編寫SO社區的示例答案。
Attribution:正則表達式的信用,100%到Russ洛斯基。在函數
代碼,刪除 - DoubleQuotesFromCsv
function Remove-DoubleQuotesFromCsv
{
param (
[Parameter(Mandatory=$true)]
[string]
$InputFile,
[string]
$OutputFile
)
if (-not $OutputFile)
{
$OutputFile = $InputFile
}
$inputCsv = Import-Csv $InputFile
$quotedData = $inputCsv | ConvertTo-Csv -NoTypeInformation
$outputCsv = $quotedData | % {$_ -replace `
'\G(?<start>^|,)(("(?<output>[^,"]*?)"(?=,|$))|(?<output>".*?(?<!")("")*?"(?=,|$)))' `
,'${start}${output}'}
$outputCsv | Out-File $OutputFile -Encoding utf8 -Force
}
測試代碼
$csvData = @"
id,string,notes,number
1,hello world.,classic,123
2,"a comma, is in here","test data 1",345
3,",a comma, is in here","test data 2",346
4,"a comma, is in here,","test data 3",347
"@
$data = $csvData | ConvertFrom-Csv
"`r`n---- data ---"
$data
$quotedData = $data | ConvertTo-Csv -NoTypeInformation
"`r`n---- quotedData ---"
$quotedData
# this regular expression comes from:
# http://www.sqlmovers.com/removing-quotes-from-csv-created-by-powershell/
$fixedData = $quotedData | % {$_ -replace `
'\G(?<start>^|,)(("(?<output>[^,"]*?)"(?=,|$))|(?<output>".*?(?<!")("")*?"(?=,|$)))' `
,'${start}${output}'}
"`r`n---- fixedData ---"
$fixedData
$fixedData | Out-File e:\test.csv -Encoding ascii -Force
"`r`n---- e:\test.csv ---"
Get-Content e:\test.csv
測試輸出
---- data ---
id string notes number
-- ------ ----- ------
1 hello world. classic 123
2 a comma, is in here test data 1 345
3 ,a comma, is in here test data 2 346
4 a comma, is in here, test data 3 347
---- quotedData ---
"id","string","notes","number"
"1","hello world.","classic","123"
"2","a comma, is in here","test data 1","345"
"3",",a comma, is in here","test data 2","346"
"4","a comma, is in here,","test data 3","347"
---- fixedData ---
id,string,notes,number
1,hello world.,classic,123
2,"a comma, is in here",test data 1,345
3,",a comma, is in here",test data 2,346
4,"a comma, is in here,",test data 3,347
---- e:\test.csv ---
id,string,notes,number
1,hello world.,classic,123
2,"a comma, is in here",test data 1,345
3,",a comma, is in here",test data 2,346
4,"a comma, is in here,",test data 3,347
我發現Kory的答案不適用於原始字符串在一行中包含多個空白字段的情況。即「ABC」,「0」很好,但「ABC」,,,「0」處理不當。它停止在「,,,」之後替換引號。我加入「|(?<output>)
」附近的第一個參數的末尾,這樣固定的:
% {$_ -replace `
'\G(?<start>^|,)(("(?<output>[^,"]*?)"(?=,|$))|(?<output>".*?(?<!")("")*?"(?=,|$))|(?<output>))', `
'${start}${output}'}
我今天在桌子上的工作和思考過這個問題,因爲我是預覽在記事本的CSV文件,並決定看看別人提出了什麼。看來許多解決方案過於複雜。
下面是一個簡單的方法,可以從PowerShell中的Export-Csv cmdlet生成的CSV文件中刪除引號。
使用以下數據創建TEST.csv文件。
「ID」, 「姓名」, 「國家」
「5」, 「斯蒂芬妮」, 「亞利桑那」
「4」, 「梅蘭妮」, 「俄勒岡州」
「2」,「凱蒂」, 「得克薩斯」
「8」, 「史蒂夫」, 「愛達荷」
「9」, 「多利」, 「田納西州」
另存爲:TEST.csv
商店文件內容在$ Tes中變量t
$測試=獲取內容。\ TEST.csv
負載測試$變量,請參閱Get-Content命令
$測試
負載測試$變量的結果再用逗號替換所有(「,」),然後通過刪除每個引號標記來修剪開始和結束
$ Test.Replace('「,」',「,」)。TrimStart(''')。TrimEnd (''')
保存/替換TEST.csv文件
$ Test.Replace( ' 「」', 「」)TrimStart( ' 「 ')TrimEnd('」')。|出文件\ TEST.csv -Force -Confirm:$假
與進口CSV和獲取內容測試新的文件輸出:
進口-CSV \ TEST.csv
獲取內容。\測試。CSV
總結這一切,工作可以用兩行代碼做
$測試=獲取內容。\ TEST.csv
$ Test.Replace( ' 「」', 「,」)。TrimStart(''')。TrimEnd('「')| Out-File。\ TEST.csv -Force -Confirm:$ false
如果您正在使用的數據因包含分隔符而被引用,則這不起作用。 –