簡體   English   中英

用powershell替換csv文件中的字符串

[英]replace a string in a csv file with powershell

我需要在csv文件中的某些列上放置System.Object []。 我嘗試了3種不同的方法,但是沒有一個起作用。 當該對象為空或某些東西時,Powershell將System.Object []放入。

$file = "c:\output.csv"

(gc $file) -replace "'system.object[]'", ""
[io.file]::readalltext($file).replace("'system.object[]'","")
(Get-Content $file | ForEach-Object { $_ -replace "system.object[]", "" } ) | Set-Content $file

我向輸出中包含System.Object []的變量添加了以下代碼。 而且似乎正在工作。 現在,我不必在文件級別進行替換。

"Access Code" = (@($AccessCode) | Out-String).Trim()

括號和點([,],。)必須全部轉義。 此外,刪除雙引號,只保留單引號。 如果您想不區分大小寫,也可以考慮使用creplace。 因此,命令如下所示:

(gc $file) -replace 'system\.object\[\]', ''

如果您想將所有內容寫入新文件:

(gc $file) -replace 'system\.object\[\]', ''|out-file "test2.txt" -encoding ASCII

只需使用轉義符

(gc $file) -replace 'system.object\[\]', ""

字符'['和']'用於Regex模式。 您必須使用轉義字符'\\'告訴Powershell這是常規字符

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM