If I got a CSV file and there are a lot of strings that contain exactly "Name".
How do I get to have only Name and not "Name"? -replace won't work as you can't quote the quotes:
$computername.Name = $computername.Name -replace (""", "") # won't work
edit: example csv export Name,"IPAddress"
SVDCO03,"10.10.15.46"
SVLIC01,"10.10.20.221"
where i need to get those quotas away
not sure why u are trying to parse the csv you can actually quote the quotes :)
enclose the double quotes inside single and use the regex replace
"name" -replace '"'
or use the string replace method.
"name".replace('"','')
Related
I am trying to create a Powershell script which wraps quotes around each columns of the file on export to CSV. However the Export-CSV applet only places these where they are needed, i.e. where the text has a space or similar within it.
I have tried to use the following to wrap the quotes on each line but it ends up wrapping three quotes on each column.
$r.SURNAME = '"'+$r.SURNAME+'"';
Is anyone able to share how to forces these on each column of the file - so far I can just find info on stripping these out.
Thanks
Perhaps a better approach would be to simply convert to CSV (not export) and then a simple regex expression could add the quotes then pipe it out to file.
Assuming you are exporting the whole object $r:
$r | ConvertTo-Csv -NoTypeInformation `
| % { $_ -replace ',(.*?),',',"$1",' } `
| Select -Skip 1 | Set-Content C:\temp\file.csv
The Select -Skip 1 removes the header. If you want the header just take it out.
To clarify what the regex expression is doing:
Match: ,(.*?),
Explanation: This will match section of each line that has a comma followed by any number of characters (.*) without being greedy (? : basically means it will only match the minimum number of characters that is needed to complete the match) and the finally is ended with a comma. The parenthesis will hold everything between the two commas in a match variable to be used later in the replace.
Replace: ,"$1",
Explanation: The $1 holds the match between the two parenthesis mention above in the match. I am surrounding it with quotes and re-adding the commas since I matched on those as well they must be replaced or they are simply consumed. Please note, that while the match portion of the -replace can have double quotes without an issue, the replace section must be surrounded in single quotes or the $1 gets interpreted by PowerShell as a PowerShell variable and not a match variable.
You can also use the following code:
$r.SURNAME = "`"$($r.SURNAME)`""
I have cheated to get what I want by re-parsing the file through the following - guess that it acts as a simple find and replace on the file.
get-content C:\Data\Downloads\file2.csv
| foreach-object { $_ -replace '"""' ,'"'}
| set-content C:\Data\Downloads\file3.csv
Thanks for the help on this.
"ID","Full Name","Age"
"1","Jone Micale","25"
Here a sample from a CSV file that I created, and now I want to remove double quotes from only the ID and Age column value.
I tried different ways but I don't want to create a new file out of it. I just want to update the file with changes using PowerShell v1.
Export-Csv will always put all fields in double quotes, so you have to remove the undesired quotes the hard way. Something like this might work:
$csv = 'C:\path\to\your.csv'
(Get-Content $csv) -replace '^"(.*?)",(.*?),"(.*?)"$', '$1,$2,$3' |
Set-Content $csv
Regular expression breakdown:
^ and $ match the beginning and end of a string respectively (Get-Content returns an array with the lines from the file).
"(.*?)" matches text between two double quotes and captures the match (without the double quotes) in a group.
,(.*?), matches text between two commas and captures the match (including double quotes) in a group.
$1,$2,$3 replaces a matching string with the comma-separated first, second and third group from the match.
I have a question about the syntax in powershell for Add-Content. My problem is, I want to add text into a textfile and that this text contains "" which is not working. For example:
Add-Content -Value "c:\Users\Administrator\Desktop\"foobar.exe""
Now the output should look like this:
c:\Users\Administrator\Desktop\"foobar.exe"
and that does not work because of these "".
Is there a way to get these "" in the -value parameter?
You can do it like this
Add-Content -Value "c:\Users\Administrator\Desktop\`"foobar.exe`"" -Path .\AA.TXT
escaping the inner ""
Or using single quote to enclose the double quote
Add-Content -Value 'c:\Users\Administrator\Desktop\"foobar.exe"' -Path .\AA.TXT
You can use a few different tricks:
Include the doubly quoted items within a singly quoted string. This makes PowerShell ignore variables, too. For instance: 'I Said "Something"'
Include the doubly quoted items with a preceding backtick. Backticks are the escape character in PowerShell, like \ in javascript or C, and this will make the " part of the string.
Use a here document . They start like this: #" and must end with a "# at the start of a line. This will still let you use variables within the quotes, and still let regular double quotes be.
For part 1, see this SO post
I have a CSV that has certain fields separated by the " symbol as a TextQualifier.
See below for example. Note that each integer (eg. 1,2,3 etc) is supposed to be a string. the qualified strings are surrounded by the " symbol.
1,2,3,"qualifiedString1",4,5,6,7,8,9,10,11,12,13,14,15,16,"qualifiedString2""
Notice how the last qualified string has a " symbol as part of the string.
User #mjolinor suggested this powershell script, which works to fix the above scenario, but it does not fix the "Part 2" scenario below.
(get-content file.txt -ReadCount 0) -replace '([^,]")"','$1' |
set-content newfile.txt
Here is part 2 of the question. I need a solution for this:
The extra " symbol can appear randomly in the string. Here's another example:
1,2,3,"qualifiedString1",4,5,6,7,8,9,10,11,12,13,14,15,16,"qualifiedS"tring2"
Can you suggest an elegant way to automate the cleaning of the CSV to eliminate redundant " qualifiers?
You just need a different regex:
(get-content file.txt -ReadCount 0) -replace '(?<!,)"(?!,|$)',''|
set-content newfile.txt
That one will replace any double quote that is not immediately preceeded by a comma, or followed by either a comma or the end of the line.
$text = '1,2,3,"qualifiedString1",4,5,6,7,8,9,10,11,12,13,14,15,16,"qualifiedS"tring2"'
$text -replace '(?<!,)"(?!,|$)',''
1,2,3,"qualifiedString1",4,5,6,7,8,9,10,11,12,13,14,15,16,"qualifiedString2"
I am trying to use PowerShell do a simple find and replace. I use template text files and use $ in front of values that need to be changed.
Example:
(Get-Content "D:\test") | Foreach-Object {$_ -replace "`$TBQUAL", "DBO"} | Set-Content "D:\test"
It should find the line OWNER=$TBQUAL and make it look like OWNER=DBO.
I am using the escape in front of $TBQUAL with no luck. To test that it is working if I removed the $ from the front, it would replace TBQUAL and made it look like OWNER=$DBO.
Two things to get this to work:
Use single quotes for your strings so that the $ is not interpreted as the start of a variable to be expanded.
Escape the $ using a backslash "\" so the regular expression parser takes it literally.
For example,
PS C:\> 'Has a $sign in it' -replace 'a \$sign', 'no dollar sign'
Has no dollar sign in it
If you aren't using regular expressions in your replacement, you can do a simple (and fast) replace like this:
Foreach-Object {$_.Replace('$TBQUAL', 'DBO')}