如何删除ubuntu上双引号内的字符?

时间:2015-03-20 14:57:28

标签: regex file ubuntu awk quotes

我的文件条件如下:

"one","two","three"" four","five"

所以我想删除双引号中的引号,所以输出如下:

"one","two","three four ","five"

如何在ubuntu上使用awk函数和正则表达式执行此操作?感谢...

3 个答案:

答案 0 :(得分:4)

您只需查找""并将其替换为空字符串即可。

像:

sed -i 's/""//' *.txt

例如:

echo '"one","two","three"" four","five"' | sed 's/""//'
"one","two","three four","five"

答案 1 :(得分:1)

sed是正确的工具。

$ echo '"one","two","three"" four","five"' | sed 's/\([^,]\)"\+\([^,]\)/\1\2/g'
"one","two","three four","five"

上面的正则表达式捕获了一个或多个双引号之前和之后退出的字符(字符而不是逗号)。所以这将匹配中心存在的双引号。

OR

$ echo '"one","two","three"" four","five"' | sed -r 's/([^,])"+([^,])/\1\2/g'
"one","two","three four","five"
  • [^,]匹配任何字符,但不匹配逗号。
  • ([^,])匹配的角色被捕获到第1组。它就像一个临时存储区。
  • "+一个或多个+
  • ([^,])捕获以下不会成为逗号的字符。
  • \1\2所有匹配的字符将替换为存储在组索引1和组索引2中的字符。

<强>更新

$ echo '"one","two","three" vg " "gfh" four","five"' | sed -r 's/([^,])"+([^,])/\1\2/g;s/([^,])"+([^,])/\1\2/g'
"one","two","three vg  gfh four","five"

答案 2 :(得分:1)

使用awk即可:

s="one","two","three"" four","five"'
awk 'BEGIN{FS=OFS=","} {for (i=1; i<=NF; i++) gsub(/""/, "", $i)} 1' <<< "$s"
"one","two","three four","five"