zoukankan      html  css  js  c++  java
  • sed 使用 删除匹配行

    “p” command prints the buffer (remember to use -n option with “p”) 
    “d” command is just opposite, its for deletion. ‘d’ will delete the pattern space buffer and immediately starts the next cycle. 

    Syntax: 
    # sed 'ADDRESS'd filename 
    # sed /PATTERN/d filename 

    Let us first creates thegeekstuff.txt file that will be used in all the examples mentioned below. 
    # cat thegeekstuff.txt 
    1. Linux - Sysadmin, Scripting etc. 
    2. Databases - Oracle, mySQL etc. 
    3. Hardware 
    4. Security (Firewall, Network, Online Security etc) 
    5. Storage 
    6. Cool gadgets and websites 
    7. Productivity (Too many technologies to explore, not much time available) 
    8. Website Design 
    9. Software Development 
    10.Windows- Sysadmin, reboot etc. 

    例子1:删除第n行 
    sed ‘Nd’ filename 

    As per sed methodology, 
    It reads the first line and places in its pattern buffer. 
    Check whether supplied command is true for this line, if true, deletes pattern space buffer and starts next cycle. i.e Read next line. 
    If supplied command doesnt true, as its normal behaviour it prints the content of the pattern space buffer. 

    $ sed 3d thegeekstuff.txt 
    1. Linux - Sysadmin, Scripting etc. 
    2. Databases - Oracle, mySQL etc. 
    4. Security (Firewall, Network, Online Security etc) 
    5. Storage 
    6. Cool gadgets and websites 
    7. Productivity (Too many technologies to explore, not much time available) 
    8. Website Design 
    9. Software Development 

    10.Windows- Sysadmin, reboot etc.

    例2:从第三行开始,每隔一行删除 
    $sed 3~2d thegeekstuff.txt 

    1. Linux - Sysadmin, Scripting etc. 
    2. Databases - Oracle, mySQL etc. 
    4. Security (Firewall, Network, Online Security etc) 
    6. Cool gadgets and websites 
    8. Website Design 
    10.Windows- Sysadmin, reboot etc. 

    $

    例3:删除从第4行到第8行 
    $sed 4,8d thegeekstuff.txt 

    1. Linux - Sysadmin, Scripting etc. 
    2. Databases - Oracle, mySQL etc. 
    3. Hardware 
    9. Software Development 
    10.Windows- Sysadmin, reboot etc. 
    $sed '4,8d' thegeekstuff.txt 
    1. Linux - Sysadmin, Scripting etc. 
    2. Databases - Oracle, mySQL etc. 
    3. Hardware 
    9. Software Development 
    10.Windows- Sysadmin, reboot etc. 
    $sed '4,8'd thegeekstuff.txt 
    1. Linux - Sysadmin, Scripting etc. 
    2. Databases - Oracle, mySQL etc. 
    3. Hardware 
    9. Software Development 

    10.Windows- Sysadmin, reboot etc.

    例4:删除最后一行 
    $sed '$'d thegeekstuff.txt 

    1. Linux - Sysadmin, Scripting etc. 
    2. Databases - Oracle, mySQL etc. 
    3. Hardware 
    4. Security (Firewall, Network, Online Security etc) 
    5. Storage 
    6. Cool gadgets and websites 
    7. Productivity (Too many technologies to explore, not much time available) 
    8. Website Design 
    9. Software Development 
    $sed '$d' thegeekstuff.txt 
    1. Linux - Sysadmin, Scripting etc. 
    2. Databases - Oracle, mySQL etc. 
    3. Hardware 
    4. Security (Firewall, Network, Online Security etc) 
    5. Storage 
    6. Cool gadgets and websites 
    7. Productivity (Too many technologies to explore, not much time available) 
    8. Website Design 

    9. Software Development

    例5:行匹配删除 
    $sed /Sysadmin/d thegeekstuff.txt 

    2. Databases - Oracle, mySQL etc. 
    3. Hardware 
    4. Security (Firewall, Network, Online Security etc) 
    5. Storage 
    6. Cool gadgets and websites 
    7. Productivity (Too many technologies to explore, not much time available) 
    8. Website Design 
    9. Software Development 
    $sed '/Sysadmin/d' thegeekstuff.txt 
    2. Databases - Oracle, mySQL etc. 
    3. Hardware 
    4. Security (Firewall, Network, Online Security etc) 
    5. Storage 
    6. Cool gadgets and websites 
    7. Productivity (Too many technologies to explore, not much time available) 
    8. Website Design 
    9. Software Development 
    $sed '/Sysadmin/'d thegeekstuff.txt 
    2. Databases - Oracle, mySQL etc. 
    3. Hardware 
    4. Security (Firewall, Network, Online Security etc) 
    5. Storage 
    6. Cool gadgets and websites 
    7. Productivity (Too many technologies to explore, not much time available) 
    8. Website Design 
    9. Software Development 

    $

    例6:从匹配行到末尾行 
    $sed '/Website Design/,$d' thegeekstuff.txt 

    1. Linux - Sysadmin, Scripting etc. 
    2. Databases - Oracle, mySQL etc. 
    3. Hardware 
    4. Security (Firewall, Network, Online Security etc) 
    5. Storage 
    6. Cool gadgets and websites 
    7. Productivity (Too many technologies to explore, not much time available) 
    $sed '/Website Design/,$'d thegeekstuff.txt 
    1. Linux - Sysadmin, Scripting etc. 
    2. Databases - Oracle, mySQL etc. 
    3. Hardware 
    4. Security (Firewall, Network, Online Security etc) 
    5. Storage 
    6. Cool gadgets and websites 

    7. Productivity (Too many technologies to explore, not much time available)

    例7:删除匹配行和之后两行 
    $sed '/Storage/,+2d' thegeekstuff.txt 

    1. Linux - Sysadmin, Scripting etc. 
    2. Databases - Oracle, mySQL etc. 
    3. Hardware 
    4. Security (Firewall, Network, Online Security etc) 
    8. Website Design 
    9. Software Development 
    10.Windows- Sysadmin, reboot etc. 
    $sed '/Storage/,+2'd thegeekstuff.txt 
    1. Linux - Sysadmin, Scripting etc. 
    2. Databases - Oracle, mySQL etc. 
    3. Hardware 
    4. Security (Firewall, Network, Online Security etc) 
    8. Website Design 
    9. Software Development 

    10.Windows- Sysadmin, reboot etc.

    例8:删除空行 
    $sed '/^$/d' thegeekstuff.txt 

    1. Linux - Sysadmin, Scripting etc. 
    2. Databases - Oracle, mySQL etc. 
    3. Hardware 
    4. Security (Firewall, Network, Online Security etc) 
    5. Storage 
    6. Cool gadgets and websites 
    7. Productivity (Too many technologies to explore, not much time available) 
    8. Website Design 
    9. Software Development 
    10.Windows- Sysadmin, reboot etc.

    每天一小步,人生一大步!Good luck~
  • 相关阅读:
    Server Tomcat v8.0 Server at localhost was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor.
    用户画像——“打标签”
    python replace函数替换无效问题
    python向mysql插入数据一直报TypeError: must be real number,not str
    《亿级用户下的新浪微博平台架构》读后感
    【2-10】标准 2 维表问题
    【2-8】集合划分问题(给定要分成几个集合)
    【2-7】集合划分问题
    【2-6】排列的字典序问题
    【2-5】有重复元素的排列问题
  • 原文地址:https://www.cnblogs.com/jkmiao/p/4862351.html
Copyright © 2011-2022 走看看