简体   繁体   English

如何删除双引号之间的双引号

[英]How to remove double quote between double quotes

How to remove double quote that is between a set of double quotes? 如何删除一组双引号之间的双引号?

"Test T"est" should get output as "Test Test" "Test T"est"应该作为"Test Test"得到输出

"Test T"est", "Test1 "Test1" should get output as "Test Test", "Test1 Test1" "Test T"est", "Test1 "Test1"应作为"Test Test", "Test1 Test1"

You can try with awk : 您可以尝试使用awk

$ awk -F", *" '{                           # Set the field separator
  for(i=1;i<=NF;i++){                      # Loop through all fields
     $i="\""gensub("\"", "", "g", $i)"\""  # Rebuild the field with only surrounding quotes
  }
}1' OFS="," file                           # Print the line 
"Test Test","Test1 Test1"

If this is a corrupted CSV and you can say there are no commas inside the fields, then PowerShell's CSV handling will read them and leave the trailing quote. 如果这是损坏的CSV,并且您可以说字段内没有逗号,那么PowerShell的CSV处理将读取它们并留下尾随引号。 Remove that, then re-export to a new CSV to get values with double quotes around them. 删除该值,然后重新导出为新的CSV,以获取带有双引号的值。

import-csv .\test.csv -Header 'column1', 'column2' | 
    ForEach-Object {

        foreach ($column in $_.psobject.properties.Name)
        {
          $_.$column = $_.$column.Replace('"', '') 
        }

        $_ 

     } | Export-Csv .\test2.csv -NoTypeInformation

If the file has headers in it, remove the -header 'column1', 'column2' part. 如果文件中包含标题,则删除-header 'column1', 'column2'部分。

So if this is for a corrupted CSV you could state the problem as remove any double quotes that don't appear at the start or end of a line and that are not near a comma (with optional white space). 因此,如果这是针对已损坏的CSV的,则您可以指出问题所在,即删除所有未出现在行首或结尾且不在逗号附近(带有可选空格)的双引号。 So this can easily be done with a Powershell regex like so: 因此,可以使用Powershell正则表达式轻松完成此操作,如下所示:

$t = '"Test T"est", "Test1 "Test1"'
$t -replace '(?<!^|\s*,\s*)"(?!\s*,\s*|$)', ''

An alternative with sed: sed的替代方法:

sed 's/\("[^"]\+\)"\([^"]\+"\)/\1\2/g' inputFile

input: 输入:

"Test T"est"
"Test T"est", "Test1 "Test1"

output: 输出:

"Test Test"
"Test Test", "Test1 Test1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM