0

在这种情况下,我想更改每个“状态”列的特殊字符之前的所有内容:

{"name":0.main","tablespace":"UNDO","size":"500","maxsize":"65536","usedsize":"29","status":.04}, {"name":0.main","tablespace":"REDO","size":"500","maxsize":"65536","usedsize":"29","status":0},

在这种情况下,我想将 .04 更改为 0.04。我尝试使用 sed:

sed 's/:./:0./g'

但第二行的结果显示“0”。在状态栏中。

如何仅更改状态列以及将.anynumber替换为0.anynumber的所有内容?

4

4 回答 4

4

更改.[digit]*0.[digit]*特定字段:

sed -r 's/("status":)(\.[0-9]+)/\10\2/' file
$猫文件
{"name":0.main","tablespace":"UNDO","size":"500","maxsize":"65536","usedsize":"29","status":.04},
{"name":0.main","tablespace":"REDO","size":"500","maxsize":"65536","usedsize":"29","status":0},

$sed -r 's/("status":)(\.[0-9]+)/\10\2/' 文件
{"name":0.main","tablespace":"UNDO","size":"500","maxsize":"65536","usedsize":"29","status":0.04},
{"name":0.main","tablespace":"REDO","size":"500","maxsize":"65536","usedsize":"29","status":0},
于 2013-06-20T12:45:18.193 回答
3

我认为awk这里更适合。您的输入由逗号分隔,键、值对在哪里status是键是第六个字段"status":.04。使用以下脚本,仅在必填字段上进行替换并将全部替换:.:0.ie 确保所有十进制值都具有前导零(如果先前不存在):

$ awk '{sub(/:[.]/,":0.",$6)}1' FS=, OFS=, file
于 2013-06-20T12:50:51.733 回答
2
$ perl -pe 's/("状态"\s*:\s*)(\.[0-9]+)(?=\s*,|\s*})/${1}0${ 2}/'输入
{"name":0.main","tablespace":"UNDO","size":"500","maxsize":"65536","usedsize":"29","status":0.04},
{"name":0.main","tablespace":"REDO","size":"500","maxsize":"65536","usedsize":"29","status":0},
于 2013-06-20T12:51:45.503 回答
1

在 GNU sed 中试试这个,

sed 's/\(.*status":.*\)\(\.[0-9]\+\)/\10\2/' file.txt

如果您想影响原始文件中的此更改,

sed -i.bak 's/\(.*status":.*\)\(\.[0-9]\+\)/\10\2/' file.txt
于 2013-06-20T12:30:39.380 回答