zoukankan      html  css  js  c++  java
  • sed_shell三剑客

    ###### 

    sed匹配正则表达式模式并替换

    标红处标示不包含“>”
    [root@sishen ~]# vim test.html
    <html><body><h1>Hello word!</h1></body></html>
    清除test.html文件中的所有html标签
    [root@sishen ~]# sed 's/<[^>]*>//g' test.html

    ######

    sed基本查找替换

    sed 's/find/replace/' $file

    sed 's/ //' 去掉空格

    sed -i "s#^$db_name.*;#$db_name='${DB_NAME}';#g" $sqlversion_file 替换整行

    ######

    sed高级命令之n、N、$!N

    sed [option] {sed-command} {input-file}
    sed在正常情况下,将处理的行读入模式空间(pattern space),脚本中的“sed-command(sed命令)”就一条接着一条进行处理,知道脚本执行完毕。然后该行呗输出,模式(pattern space)被清空;接着,在重复执行刚才的动作,文件中的新的一行被读入,直到文件处理完毕。

    但是,由于种种原因,如用户希望在某个条件下,脚本中的某个命令被执行或希望模式空间(pattern space)保留,以便下一次使用,这都有可能使sed在处理文件的时候,不按照正常的流程来进行处理,这时候就需要用sed高级命令来满足需求。
     
    先来说说命令n和命令N
    n命令:读取下一行到pattern space。由于pattern space中有按照正常流程读取的内容,使用n命令后,pattern space中又有了一行,此时,pattern space中有2行内容,但是先读取的那一行不会被取代、覆盖或删除;当n命令后,还有其他命令p的时候,此时打印出的结果是n命令读取的那一行的内容。
    N命令:将下一行添加到pattern space中。将当前读入行和用N命令添加的下一行看成“一行”。
    $!N命令:最后一行不执行N命令。

    ######

    sed高级命令之a

    sed '/hello/aword' westos 表示把word加在含有hello后一行  默认是在后面一行追加
    [root@nfvo_single ~]# cat test.txt
    ddd
    [root@nfvo_single ~]# sed '/ddd/aabuse' test.txt
    ddd
    abuse

  • 相关阅读:
    HDU 1069 Monkey and Banana
    HDU 1029 Ignatius and the Princess IV
    HDU 1024 Max Sum Plus Plus
    Gym100923H Por Costel and the Match
    Codeforces 682C Alyona and the Tree
    Codeforces 449B Jzzhu and Cities
    Codeforces (ccpc-wannafly camp day2) L. Por Costel and the Semipalindromes
    Codeforces 598D (ccpc-wannafly camp day1) Igor In the Museum
    Codeforces 1167c(ccpc wannafly camp day1) News Distribution 并查集模板
    快乐数问题
  • 原文地址:https://www.cnblogs.com/kingcs/p/14586447.html
Copyright © 2011-2022 走看看