英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论