sed -i 保留中间部分不被替换
目标:去掉vaule 值上面的"" 引号,中间保持不变 test01.txt
"value": "[{\"registrationType\":\"TaxId\",\"description\":\"Tax ID\",\"taxNumberType\":\"CPI-TEST\",\"registrationFormat\":\"DE[0-9]{9}\",\"primary\":[\"Registered Corporation\",\"Private Person\"]},{\"registrationType\":\"TaxId\",\"description\":\"Tax ID\",\"taxNumberType\":\"CPI-SSW\",\"registrationFormat\":\"DE[0-9]{4}\",\"primary\":[\"Registered Corporation\",\"Private Person\"]}]"
变成如下:
"value": [{\"registrationType\":\"TaxId\",\"description\":\"Tax ID\",\"taxNumberType\":\"CPI-TEST\",\"registrationFormat\":\"DE[0-9]{9}\",\"primary\":[\"Registered Corporation\",\"Private Person\"]},{\"registrationType\":\"TaxId\",\"description\":\"Tax ID\",\"taxNumberType\":\"CPI-SSW\",\"registrationFormat\":\"DE[0-9]{4}\",\"primary\":[\"Registered Corporation\",\"Private Person\"]}]
方法:使用sed -i 的正则保留内容的语法
使用正则表达式 \(..\)表示保留括号内的内容不被替换掉。使用\1或者\2(按照顺序保留下来)
sed -i 's/"value": "\(.*\)]"/"value": \1\] /g' test01.txt
其中,\1就是\(..\)这个保留下来的东西,由于它是第一个保留下来的,因此被标记为\1,如果有多个的话,就是\2,\3,往后排。
\1就表示这个部分:
[{\"registrationType\":\"TaxId\",\"description\":\"Tax ID\",\"taxNumberType\":\"CPI-TEST\",\"registrationFormat\":\"DE[0-9]{9}\",\"primary\":[\"Registered Corporation\",\"Private Person\"]},{\"registrationType\":\"TaxId\",\"description\":\"Tax ID\",\"taxNumberType\":\"CPI-SSW\",\"registrationFormat\":\"DE[0-9]{4}\",\"primary\":[\"Registered Corporation\",\"Private Person\"]}
注意没有最后的]这个哦!