从特定字段中删除引号 - 引号之间的整数

时间:2015-06-14 10:55:52

标签: regex linux awk sed

我想从特定字段中删除引号之间存在整数的引号。

从这一行:"AAA","99"

到这一行:"AAA",99

3 个答案:

答案 0 :(得分:3)

使用sed

s='"AAA","99"'
sed 's/"\([0-9]*\)"/\1/g' <<< "$s"
"AAA",99

答案 1 :(得分:2)

尝试使用GNU sed:

echo '"AAA","99"' | sed -E 's/"([0-9]+)"$/\1/'

输出:

"AAA",99

答案 2 :(得分:1)

使用awk:

awk -F , 'BEGIN { OFS = FS } $2 ~ /^"[0-9]+"$/ { gsub(/"/, "", $2) } 1'

其工作原理如下:

BEGIN { OFS = FS }  # Delimit output the same way as the input

$2 ~ /^"[0-9]+"$/ { # if the second field ($2) consists of only numbers encased
                    # by double quotes (i.e., matches the regex ^"[0-9]"$)
  gsub(/"/, "", $2) # remove the quotes from it
}
1                   # print in any case

要使其适用于任何(而不仅仅是特定字段),请在循环中进行替换:

awk -F , 'BEGIN { OFS = FS } { for(i = 1; i <= NF; ++i) if($i ~ /^"[0-9]+"$/) { gsub(/"/, "", $i) } } 1'

此处更改的位是

{                              # Do this unconditionally in every line:
  for(i = 1; i <= NF; ++i) {   # wade through all the fields
    if($i ~ /^"[0-9]+"$/) {    # then do with them the same thing as before.
      gsub(/"/, "", $i)
    }
  }
}