sed 正则的一个小问题

有一段类似以下的文本

aabbccc test[3307]112323553-66778tp aooppx69tp  ooppsg
aabbccc test[3307]1127233-6674tp booppx67tp  oofs3g
aabbccc test[3307]1125233-6277558tp cooppx65tp  xxppsg

希望将上面第二个字段的内容全部替换为mysql[3306]2018xx-3306xb

一开始,使用下面的命令:

sed -i 's#\<test\[.*\].*\-.*tp\>#mysql[3306]2018xx-3306xb#g'  test.html  

结果导致中间的内容全被替换:

aabbccc mysql[3306]2018xx-3306xb  ooppsg
aabbccc mysql[3306]2018xx-3306xb  oofs3g
aabbccc mysql[3306]2018xx-3306xb  xxppsg

后来改为:

sed -i 's#\<test\[.*\][0-9]+\-[0-9]+tp\>#mysql[3306]2018xx-3306xb#g'  test.html

没用,再调整为:

sed -i 's#\<test\[.*\][0-9]\+\-[0-9]\+tp\>#mysql[3306]2018xx-3306xb#g'  test.html 

就可以了

aabbccc mysql[3306]2018xx-3306xb aooppx69tp  ooppsg
aabbccc mysql[3306]2018xx-3306xb booppx67tp  oofs3g
aabbccc mysql[3306]2018xx-3306xb cooppx65tp  xxppsg

第一条命令没有完全精确到想要的字段,第二条命令来看,sed中的+似乎需要加个****在前面?

猜你喜欢

转载自www.cnblogs.com/wshenjin/p/9885361.html
sed