zoukankan      html  css  js  c++  java
  • sed命令总结-基本操作指南

    sed是一种流编辑器,能够完美的配合正则表达式使用。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。
    命令语法:

    sed [OPTION]... {script-only-if-no-other-script} [input-file]...

    常用参数说明:
    sed的参数可以分为参数和动作2个类别,参数不用说,动作即表示对文件进行哪些处理
    参数:

    -e                    一行命令语句,可以执行多条sed命令
    -f                    后面接sed脚本的文件名
    -h                    显示帮助文档
    -n                    取消默认的sed输出
    -V                    显示sed版本

    动作说明:

    a                    在当前行下面插入文本
    d                    删除指定行,可以是多行如2,5表示删除2-5行
    -i                    将处理结果insert到文件中,会修改文件内容,慎用
    p                    打印文件内容,
    s                    替换,用于修改文件内容,通常与g参数一起使用,g表示整行替换。
    c                    替换指定行

    特殊参数:

    ^                 匹配行开始,如:/^sed/匹配所有以sed开头的行。
    $                 匹配行结束,如:/sed$/匹配所有以sed结尾的行。
    .                 匹配一个非换行符的任意字符,如:/s.d/匹配s后接一个任意字符,最后是d。
    *               匹配0个或多个字符,如:/*sed/匹配所有模板是一个或多个空格后紧跟sed的行。
    []                 匹配一个指定范围内的字符,如/[ss]ed/匹配sed和Sed。  
    [^]             匹配一个不在指定范围内的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一个字母开头,紧跟ed的行。
    (..)             匹配子串,保存匹配的字符,如s/(love)able/1rs,loveable被替换成lovers。
    &                 保存搜索字符用来替换其他字符,如s/love/**&**/,love这成**love**<                 匹配单词的开始,如:/<love/匹配包含以love开头的单词的行。
    >                 匹配单词的结束,如/love>/匹配包含以love结尾的单词的行。
    x{m}             重复字符x,m次,如:/0{5}/匹配包含5个0的行。
    x{m,}         重复字符x,至少m次,如:/0{5,}/匹配至少有5个0的行。
    x{m,n}         重复字符x,至少m次,不多于n次,如:/0{5,10}/匹配5~10个0的行

    实际案列展示:
    原文件内容:

    [root@Server test]# cat test.txt 
    111
    222
    333
    444
    555
    666

    1、在文件的第5行后面添加一行数据

    [root@Server test]# sed -e 5a666 test.txt 
    111
    222
    333
    444
    555
    666

    2、删除、添加指定行内容(a、d动作)

    1)删除文件第二行至第五行的数据

    [root@Server test]# sed '2,5d' test.txt
         1    111
         6    666

    2)删除第二行至最后有一行的数据

    [root@Server test]# sed '2,$d' test.txt 
    111

    3)在第二行后面添加一句‘hello world’

    [root@Server test]# sed '2a hello world' test.txt 
    111
    222
    hello world
    333
    444
    555
    666

    4)在第二行后面添加2句话

    [root@Server test]# sed '2a hello world
    hello WOW' test.txt 
    111
    222
    hello world
    hello WOW
    333
    444
    555
    666

    此处用 进行的换行操作。

    4、文件替换(s动作)替换文件中符合替换规则的内容

    1)只替符合规则的行内的第一个数据

    [root@Server test]# sed 's/333/aaa/' test.txt 
    111
    222
    aaa  444 333
    444
    555
    666
    111

    2)替换整行内符合规则的数据

    [root@Server test]# sed 's/333/aaa/g' test.txt 
    111
    222
    aaa  444 aaa
    444
    555
    666
    111

    5、sed多点编辑(e参数)将文件中的111替换为777,并在第6行后面添加999

    [root@Server test]# sed -e 's/111/777/g' -e '6a999' test.txt 
    777
    222
    333
    444
    555
    666
    999
    777

    6、替换指定行内容(c动作)将第6行替换为222

    [root@Server test]# sed -e '6c222' test.txt 
    111
    222
    333
    444
    555
    222
    111

    7、删除文件第一行数据,并写入文件

    [root@Server test]# cat test.txt 
    111
    222
    333
    444
    555
    666
    [root@Server test]# sed -i '2d' test.txt 
    [root@Server test]# cat test.txt 
    111
    333
    444
    555
    666

    -i参数可以直接修改文件内容,如果只是单纯的分析文件数据,请不要用。一般用于文件内容替换,修改等操作。比如一下案例:

    1)永久关闭linux的SELinux服务

    [root@Server test]# cat /etc/selinux/config 
    
    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #     enforcing - SELinux security policy is enforced.
    #     permissive - SELinux prints warnings instead of enforcing.
    #     disabled - No SELinux policy is loaded.
    SELINUX=enforcing
    # SELINUXTYPE= can take one of three two values:
    #     targeted - Targeted processes are protected,
    #     minimum - Modification of targeted policy. Only selected processes are protected. 
    #     mls - Multi Level Security protection.
    SELINUXTYPE=targeted 
    [root@Server test]# sed -i 's/enforcing/disabled/g' /etc/selinux/config
    [root@Server test]# cat /etc/selinux/config 
    
    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #     disabled - SELinux security policy is enforced.
    #     permissive - SELinux prints warnings instead of disabled.
    #     disabled - No SELinux policy is loaded.
    SELINUX=disabled
    # SELINUXTYPE= can take one of three two values:
    #     targeted - Targeted processes are protected,
    #     minimum - Modification of targeted policy. Only selected processes are protected. 
    #     mls - Multi Level Security protection.
    SELINUXTYPE=targeted 
  • 相关阅读:
    ABAP 获取当天的上一个工作日或下一个工作日
    ABAP 增强实战:Enhancement Implementation增强点实施例子
    ABAP Alv输出金额字段时,需要按国家的货币格式显示,列如:JPY
    ABAP 调用程序时获取的数量,金额和日期字段会出现 逗号,-,负号等非法字段,所需要进行转化
    ABAP 调用标准报表程序,获取程序输出list
    ABAP Alv Varient问题:可以更改alv字段布局然后存到Varient中
    ABAP 向下取整和向上取整及取余数
    传统视觉处理方法笔记
    图像特征与描述笔记
    图像预处理笔记
  • 原文地址:https://www.cnblogs.com/cangyuefeng/p/9013842.html
Copyright © 2011-2022 走看看