在这种情况下,我希望在每个“状态”列的特殊字符之前更改所有内容:
{ “名”:0.main”, “表”: “撤销”, “大小”: “500”, “最大范围”: “65536”, “usedsize”: “29”, “状态”:04}, { “名称”:0.main”, “表”: “重做”, “尺寸”: “500”, “MAXSIZE”: “65536”, “usedsize”: “29”, “状态”:0},< / p>
在这种情况下,我想将.04更改为0.04。我尝试用sed:
sed 's/:./:0./g'
但第二行的结果显示为“0”。在状态栏中。
如何仅更改状态列以及 .anynumber 的所有内容,以替换为 0.anynumber ?
答案 0 :(得分:4)
使用sed将特定字段的.[digit]*
更改为0.[digit]*
:
sed -r 's/("status":)(\.[0-9]+)/\10\2/' file
$cat 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/' file {"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},
答案 1 :(得分:3)
我认为awk
在这里更适合。您的输入以逗号分隔,status
为键的键值对是第六个字段"status":.04
。使用以下脚本,替换仅发生在必填字段上,并将所有:.
替换为:0.
,即确保所有十进制值在以前不存在时具有前导零:
$ awk '{sub(/:[.]/,":0.",$6)}1' FS=, OFS=, file
答案 2 :(得分:2)
$ perl -pe 's/("status"\s*:\s*)(\.[0-9]+)(?=\s*,|\s*})/${1}0${2}/' input {"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},
答案 3 :(得分:1)
在GNU sed中尝试这个,
sed 's/\(.*status":.*\)\(\.[0-9]\+\)/\10\2/' file.txt
如果您想影响原始文件中的此更改,
sed -i.bak 's/\(.*status":.*\)\(\.[0-9]\+\)/\10\2/' file.txt