我在sed中指定“{”和“}”之间的地址空间,所以我希望只将第一个“Acer”替换为“TTTT”。第二个是不期望的。我该如何解决这个问题? 我在Ubuntu 15.10上测试过,sed版本是sed(GNU sed)4.2.2。
提前致谢。
$ echo "
[
{
\"manufacturer\": \"Acer\",
\"regularPrice\": 165.99,
},
[
\"manufacturer\": \"Acer\",
\"regularPrice\": 165.99,
],
{
\"manufacturer\": \"Acer\",
\"regularPrice\": 165.99,
}
]
" | sed "/{/,/},/ {s/\"Acer\"/\"TTTT\"/}"
其结果如下:
[
{
"manufacturer": "TTTT",
"regularPrice": 165.99,
},
[
"manufacturer": "Acer",
"regularPrice": 165.99,
],
{
"manufacturer": "TTTT",
"regularPrice": 165.99,
}
]
答案 0 :(得分:1)
这仅适用于GNU sed
sed "/{/,/},/ {0,/\"Acer\"/ s/\"Acer\"/\"TTTT\"/}"
或更确切地说,以下内容也在起作用
sed "/{/,/},/ {0,/\"Acer\"/s//\"TTTT\"/}"
答案 1 :(得分:1)
echo '
[
{
"manufacturer": "Acer",
"regularPrice": 165.99,
},
[
"manufacturer": "Acer",
"regularPrice": 165.99,
],
{
"manufacturer": "Acer",
"regularPrice": 165.99,
}
]
' | perl -0777 -pe 's/({[^}]*)"Acer"([^{]*?},)/$1"TTTT"$2/gs'
我在这里使用了perl v5.16.3,它返回以下内容:
[
{
"manufacturer": "TTTT",
"regularPrice": 165.99,
},
[
"manufacturer": "Acer",
"regularPrice": 165.99,
],
{
"manufacturer": "Acer",
"regularPrice": 165.99,
}
]
注意: