zoukankan      html  css  js  c++  java
  • 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中的+似乎需要加个在前面?

  • 相关阅读:
    16 Errors and Exceptions
    13 Asynchronous Programming
    wpf入门
    iframe cross domain
    ini_set("error_log",$error_path)设置
    MySQL中merge表存储引擎用法
    php中静态属性静态方法的用法
    PHP的SESSIONID生成原理
    composer的基本 使用
    相邻省份数组
  • 原文地址:https://www.cnblogs.com/wshenjin/p/9885361.html
Copyright © 2011-2022 走看看