使用PowerShell从.csv文件中删除特定行中的特定字符

huangapple go评论55阅读模式
英文:

Removing specific character on rows in .csv file using PowerShell

问题

Mimi Maryami|Single|81384482360|3602054303800001|4300018742|004204300018742|643100|APN0NCC1A3|03/02/2023|03/01/2024|Jl KP panyembir RT 001/001 Serdang Kulon Panongan Tangerang 15710|Serdang Kulon|15710|36944181|NEW| 2.0|Toko Kharisma 88|Tangerang|AMAN_PPI

$Path = "D:\WORK\Task - Script221010 - AJK - ITPRODIS380 - upload file csv ke sql server\csvfile\"
$file_name = "testing.csv"
$csv_file = $Path + $file_name
(Get-Content $csv_file) -replace '\|Toko Kharisma "88"\|', '|Toko Kharisma 88|' |
Set-Content $csv_file

This code will remove the double quotes from the specified part in your CSV file.

英文:
Mimi Maryami|Single|81384482360|3602054303800001|4300018742|004204300018742|643100|APN0NCC1A3|03/02/2023|03/01/2024|Jl KP panyembir RT 001/001 Serdang Kulon Panongan Tangerang 15710|Serdang Kulon|15710|36944181|NEW| 2.0|Toko Kharisma "88"|Tangerang|AMAN_PPI

Here a sample from my CSV file, and I want to remove double quotes from |Toko Kharisma "88"|

$Path = "D:\WORK\Task - Script221010 - AJK - ITPRODIS380 - upload file csv ke sql server\csvfile\"
$file_name = "testing.csv" 
$csv_file = $Path+$file_name 
(Get-Content $csv_file) -replace '^"(.*?)"$', '$1' |
Set-Content $csv_file

I have tried using the following code, but it still doesn't work, I just want to update it not create a new file

答案1

得分: 0

这可以起到作用,我在我的一侧进行了测试:

$CSV文件= 'E:\temp\CSV1.csv'
$Temp文件= "$Env:Temp\Temp.csv"
$(GC $CSV文件) -替换 '\x22''' | Set-Content $Temp文件
#如果您真的想之后替换原始文件,那么您可以像这样覆盖它:
Move-Item -路径 $Temp文件 -目的地 $CSV文件 -Force
GC $CSV文件
英文:

This can do the trick, tested on my side:

$CSVFile = 'E:\temp\CSV1.csv'
$TempFile = "$Env:Temp\Temp.csv"
$(GC $CSVFile) -Replace '\x22','' | Set-Content $TempFile
#If you really want to replace the original afterwards, then you can over-write it like this:
Move-Item -Path $TempFile -Destination $CSVFile -Force
GC $CSVFile

huangapple
  • 本文由 发表于 2023年1月9日 16:11:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定