[英]How to fix a double/single quote inside the quotes in a MySQL 8 with BASH with EOF?
[英]How to remove double quote between double quotes
如何删除一组双引号之间的双引号?
"Test T"est"
应该作为"Test Test"
得到输出
"Test T"est", "Test1 "Test1"
应作为"Test Test", "Test1 Test1"
您可以尝试使用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"
如果这是损坏的CSV,并且您可以说字段内没有逗号,那么PowerShell的CSV处理将读取它们并留下尾随引号。 删除该值,然后重新导出为新的CSV,以获取带有双引号的值。
import-csv .\test.csv -Header 'column1', 'column2' |
ForEach-Object {
foreach ($column in $_.psobject.properties.Name)
{
$_.$column = $_.$column.Replace('"', '')
}
$_
} | Export-Csv .\test2.csv -NoTypeInformation
如果文件中包含标题,则删除-header 'column1', 'column2'
部分。
因此,如果这是针对已损坏的CSV的,则您可以指出问题所在,即删除所有未出现在行首或结尾且不在逗号附近(带有可选空格)的双引号。 因此,可以使用Powershell正则表达式轻松完成此操作,如下所示:
$t = '"Test T"est", "Test1 "Test1"'
$t -replace '(?<!^|\s*,\s*)"(?!\s*,\s*|$)', ''
sed的替代方法:
sed 's/\("[^"]\+\)"\([^"]\+"\)/\1\2/g' inputFile
输入:
"Test T"est"
"Test T"est", "Test1 "Test1"
输出:
"Test Test"
"Test Test", "Test1 Test1"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.