Jump to content

Removing double quotes from CSV


Go to solution Solved by Skeletor,

Recommended Posts

Posted

Hi All,

I've been staring at this for long and have no idea how to resolve this.

I have a csv that Powershell created. It produces double quotes around the file. Im want to remove the double quotes using StringRegExpReplace.

$PShell_Path = "GetIPAddress.PS1"
FileWrite("GetIPAddress.PS1", "Get-NetIPAddress -AddressFamily IPv4 | Select-Object InterfaceAlias, IPAddress | Export-Csv -Path 'results.csv' -NoTypeInformation")
$command = "powershell.exe -ExecutionPolicy Bypass -File " & $PShell_Path
Run(@ComSpec & " /c " & $command, "", @SW_HIDE)

; Read the contents of the CSV file into a string
Global $sContents = FileRead("results.csv")

; Replace all double quotes with a single quote using StringRegExpReplace()
$sContents = StringRegExpReplace($sContents, '"', "'", "G")

; Write the modified contents back to the CSV file
FileWrite("results.csv", $sContents)

I dont see the result, $sContents provides a blank file or doesnt even write to the file.

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

Posted

Your syntax seems to be not correct. 4th parameter is "G", but it should be a "count" parameter:

 

StringRegExpReplace ( "test", "pattern", "replace" [, count = 0] )

Maybe some error handling hepls (check @error). 

 

 

Rule #1: Always do a backup         Rule #2: Always do a backup (backup of rule #1)

  • Solution
Posted (edited)

Thanks @ajag, removed it, but the actual issue was infact a delay in file creation.

I used RunWait and used brackets in my regex for the quotes and this resolved my issue.

$PShell_Path = "GetIPAddress.PS1"
FileWrite("GetIPAddress.PS1", "Get-NetIPAddress -AddressFamily IPv4 | Select-Object InterfaceAlias, IPAddress | Export-Csv -Path 'results.csv' -NoTypeInformation")
$command = "powershell.exe -ExecutionPolicy Bypass -File " & $PShell_Path
RunWait(@ComSpec & " /c " & $command, "", @SW_HIDE)

; Read the contents of the CSV file into a string
Global $sContents = FileRead("results.csv")
; Replace all double quotes with a single quote using StringRegExpReplace()
$sContents = StringRegExpReplace($sContents, '["]', "")
; Write the modified contents back to the CSV file
FileOpen("results.csv", 2)
FileWrite("results.csv", $sContents)
FileClose("results.csv")

 

Edited by Skeletor

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

Posted

Hi Skeletor
Glad you solved your issue. I got a question : why are the brackets needed ?

If I run the following script, then I got the same result, with or without brackets :

$sSubject = '"123"' & "|" & '"456"'
ConsoleWrite($sSubject & @crlf & @crlf)

$sContent1 = StringRegExpReplace($sSubject, '"', "")
ConsoleWrite($sContent1 & @crlf)

$sContent2 = StringRegExpReplace($sSubject, '["]', "")
ConsoleWrite($sContent2 & @crlf)

Scite Console :

"123"|"456"

123|456
123|456

 

"I think you are searching a bug where there is no bug..."

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...