有一段类似以下的文本

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中的+似乎需要加个\在前面?

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
  • 2022-01-17
  • 2022-02-04
猜你喜欢
  • 2021-05-29
  • 2021-09-09
  • 2022-12-23
  • 2021-10-28
  • 2022-12-23
  • 2022-12-23
  • 2021-06-10
相关资源
相似解决方案