zoukankan      html  css  js  c++  java
  • sed

    Linux sed 命令是利用脚本来处理文本文件。

    sed 可依照脚本的指令来处理、编辑文本文件。

    Sed 主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等。

    参数说明

    • -e<script>或--expression=<script> 以选项中指定的script来处理输入的文本文件。
    • -f<script文件>或--file=<script文件> 以选项中指定的script文件来处理输入的文本文件。
    • -h或--help 显示帮助。
    • -n或--quiet或--silent 仅显示script处理后的结果。
    • -V或--version 显示版本信息

    动作说明

    • a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
    • c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
    • d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
    • i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
    • p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
    • s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!

    1. sed -e 1a添加一行  /root/tmp/words.txt

    [root@Server-n93yom tmp]# nl words.txt
         1    the day is sunny the the
    
         2    hello java
         3    he sunny is is
    [root@Server-n93yom ~]# sed -e 1a添加一行 /root/tmp/words.txt
    the day is sunny the the
    添加一行
    
    hello java
    he sunny is is

     1a意思为在第一行后面添加一行 不是真实添加

    2.nl -b a words.txt | sed '2d' 删除第二行

    [root@Server-n93yom tmp]# nl -b a words.txt
         1    the day is sunny the the
         2
         3    hello java
         4    he sunny is is
    [root@Server-n93yom tmp]# nl -b a words.txt | sed '2d'
         1    the day is sunny the the
         3    hello java
         4    he sunny is is

    3. nl -b a words.txt | sed '2,$d'  从第二行删除到最后一行

    [root@Server-n93yom tmp]# nl -b a words.txt | sed '2d'
         1    the day is sunny the the
         3    hello java
         4    he sunny is is
    [root@Server-n93yom tmp]# nl -b a words.txt | sed '2,$d'
         1    the day is sunny the the

    4.nl -b a words.txt | sed '2a test123'  在第二行后添加 test123

    [root@Server-n93yom tmp]# nl -b a  words.txt | sed '2a test123'
         1    the day is sunny the the
         2
    test123
         3    hello java
         4    he sunny is is

    5.nl words.txt | sed '1,4c haha' 第1到第4行,用haha替代

    [root@Server-n93yom tmp]# nl words.txt | sed '1,4c haha'
    haha

    6.nl words.txt | sed -n '3,4p'   打印第3和第4行数据

    [root@Server-n93yom tmp]# nl words.txt | sed -n '3,4p'
         2    hello java
         3    he sunny is is

    7.nl words.txt | sed -n '/hello/p'  打印只包含hello的行

    [root@Server-n93yom tmp]# nl words.txt | sed -n '/hello/p'
         2    hello java

    8.nl words.txt | sed  '/hello/d'   删除hello的匹配行,并打印剩下的行

    [root@Server-n93yom tmp]# nl words.txt | sed  '/hello/d'
         1    the day is sunny the the
    
         3    he sunny is is

    9.nl words.txt | sed  's/hello/guan/g'  搜索hello并替换为guan       sed 's/要被取代的字串/新的字串/g'

    [root@Server-n93yom tmp]# nl words.txt | sed  's/hello/guan/g'
         1    the day is sunny the the
    
         2    guan java
         3    he sunny is is

    10. nl words.txt | grep hello | sed 's/java//g'  找出hello的行,并把后面的java去除

    [root@Server-n93yom tmp]# nl words.txt | grep hello | sed 's/java//g'
         2    hello

    11.sed -i '$a # This is a test' regular_express.txt   -i会真正的对文件进行编辑,所以对于系统文件要谨慎使用

    [root@Server-n93yom tmp]# sed -i '$a # This is a test' words.txt
    [root@Server-n93yom tmp]# nl words.txt
         1    the day is sunny the the
    
         2    hello java
         3    he sunny is is
         4    # This is a test

    sed 的 -i 选项可以直接修改文件内容,这功能非常有帮助!举例来说,如果你有一个 100 万行的文件,你要在第 100 行加某些文字,此时使用 vim 可能会疯掉!因为文件太大了

    以上都是自己手敲一遍的,如若有误请指正,谢谢

    参考:https://www.runoob.com/linux/linux-comm-sed.html

  • 相关阅读:
    WCF – System.ServiceModel.FaultException
    SSB – Connection handshake failed
    JQuery语法总结和注意事项
    删除指定目录及其子目录和文件
    Windows 7中还不能通过“HyperV管理器”连接HyperV Server 2008 R2服务器解决
    tomcat虚拟目录映射网络共享目录的问题
    Creating a Virtual Directory with a UNC Path (IIS 6.0)
    百度地图api根据地区名称反查其经纬度的实例
    远程管理server core上的hyperv
    ios5中使用Reachability的问题
  • 原文地址:https://www.cnblogs.com/guanbin-529/p/11441009.html
Copyright © 2011-2022 走看看