sed replace不适用于特定的正则表达式

时间:2019-09-13 14:24:21

标签: sed

我正在尝试用sed代替:

cat myfile | grep router | sed -e 's/Custom devices \(DiY\) \[CC2530 router\]\(http:\/\/ptvo\.info\/cc2530-based-zigbee-coordinator-and-router-112\/\) \(CC2530\.ROUTER\)/CC2530 router/g'

管道grep的输出是:

<text text-anchor="middle" x="455.5" y="-31.3" font-family="Times,serif" font-size="14.00" fill="#ffffff">Custom devices (DiY) [CC2530 router](http://ptvo.info/cc2530&#45;based&#45;zigbee&#45;coordinator&#45;and&#45;router&#45;112/) (CC2530.ROUTER)</text>

效果很好:

cat myfile | grep Xiaomi | sed -e 's/Xiaomi Aqara temperature, humidity and pressure sensor/AqaraTHP/g'

我在这里想念什么?

1 个答案:

答案 0 :(得分:2)

手册页上说

  

-e命令

     

将command参数指定的编辑命令附加到   命令列表。

您需要使用-E-r选项(您的sed支持的选项):

cat myfile | grep router | sed -E 's/Custom devices \(DiY\) \[CC2530 router\]\(http:\/\/ptvo\.info\/cc2530&#45;based&#45;zigbee&#45;coordinator&#45;and&#45;router&#45;112\/\) \(CC2530\.ROUTER\)/CC2530 router/g'

-E中,\(表示一个简单的(符号,(是捕获组的指示符。