zoukankan      html  css  js  c++  java
  • Linux学习(二十四)正则表达式(二)sed

    sed

    grep工具的功能其实还不够强大,grep实现的只是查找功能,而它却不能实现把查找的内容替换掉。以前用vim的时候,可以查找也可以替换,但是只局限于在文本内部来操作,而不能输出到屏幕上。sed工具以及下面要讲的awk工具就能实现把替换的文本输出到屏幕上的功能了,而且还有其他更丰富的功能。sed和awk都是流式编辑器,是针对文档的行来操作的。

    1.打印某行

    sed -n 'n'p filename 单引号内的n是一个数字,表示第几行:

    [root@localhost ~]# sed -n '2'p /etc/passwd
    bin:x:1:1:bin:/bin:/sbin/nologin

    要想把所有行都打印出来可以使用 sed -n '1,$'p filename

    [root@localhost ~]# sed -n '1,$'p test.txt
    rot:x:0:0:/rot:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin
    operator:x:11:0:operator:/rooot:/sbin/nologin
    roooot:x:0:0:/rooooot:/bin/bash
    1111111111111111111111111111111
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

    也可以指定一个区间:

    [root@localhost ~]# sed -n '1,3'p test.txt
    rot:x:0:0:/rot:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin
    operator:x:11:0:operator:/rooot:/sbin/nologin

    2.打印包含某个字符串的行

    [root@localhost ~]# sed -n '/root/'p test.txt
    operator:x:11:0:operator:/root:/sbin/nologin

    grep中使用的特殊字符,如 ^ $ . * 等同样也能在sed中使用

    [root@localhost ~]# sed -n '/^1/'p test.txt
    1111111111111111111111111111111
    [root@localhost ~]# sed -n '/in$/'p test.txt
    operator:x:11:0:operator:/root:/sbin/nologin
    operator:x:11:0:operator:/rooot:/sbin/nologin
    [root@localhost ~]# sed -n '/r..o/'p test.txt
    operator:x:11:0:operator:/root:/sbin/nologin
    operator:x:11:0:operator:/rooot:/sbin/nologin
    roooot:x:0:0:/rooooot:/bin/bash
    [root@localhost ~]# sed -n '/ooo*/'p test.txt
    operator:x:11:0:operator:/root:/sbin/nologin
    operator:x:11:0:operator:/rooot:/sbin/nologin
    roooot:x:0:0:/rooooot:/bin/bash

    3 -e可以实现多个行为

    [root@localhost ~]# sed -e '1'p -e '/111/'p -n test.txt
    rot:x:0:0:/rot:/bin/bash
    1111111111111111111111111111111

    4.删除某行或者多行

    [root@localhost ~]# sed '1'd test.txt
    operator:x:11:0:operator:/root:/sbin/nologin
    operator:x:11:0:operator:/rooot:/sbin/nologin
    roooot:x:0:0:/rooooot:/bin/bash
    1111111111111111111111111111111
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    [root@localhost ~]# sed '1,3'd test.txt
    roooot:x:0:0:/rooooot:/bin/bash
    1111111111111111111111111111111
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    [root@localhost ~]# sed '/oot/'d test.txt
    rot:x:0:0:/rot:/bin/bash
    1111111111111111111111111111111
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

    ‘d’ 这个字符就是删除的动作了,不仅可以删除指定的单行以及多行,而且还可以删除匹配某个字符的行,另外还可以删除从某一行一直到文档末行。

    5.替换字符或字符串

    [root@localhost ~]# sed '1,2s/ot/to/g' test.txt
    rto:x:0:0:/rto:/bin/bash
    operator:x:11:0:operator:/roto:/sbin/nologin
    operator:x:11:0:operator:/rooot:/sbin/nologin
    roooot:x:0:0:/rooooot:/bin/bash
    1111111111111111111111111111111
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

    上例中的 ‘s’ 就是替换的命令, ‘g’ 为本行中全局替换,如果不加 ‘g’ 只换该行中出现的第一个。除了可以使用 ‘/’ 作为分隔符外,还可以使用其他特殊字符例如 ‘#’ 或者 ‘@’ 都没有问题。

    [root@localhost ~]# sed 's#ot#to#g' test.txt
    rto:x:0:0:/rto:/bin/bash
    operator:x:11:0:operator:/roto:/sbin/nologin
    operator:x:11:0:operator:/rooto:/sbin/nologin
    roooto:x:0:0:/rooooto:/bin/bash
    1111111111111111111111111111111
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    [root@localhost ~]# sed 's@ot@to@g' test.txt
    rto:x:0:0:/rto:/bin/bash
    operator:x:11:0:operator:/roto:/sbin/nologin
    operator:x:11:0:operator:/rooto:/sbin/nologin
    roooto:x:0:0:/rooooto:/bin/bash
    1111111111111111111111111111111
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

    现在思考一下,如何删除文档中的所有数字或者字母?

    [root@localhost ~]# sed 's/[0-9]//g' test.txt
    rot:x:::/rot:/bin/bash
    operator:x:::operator:/root:/sbin/nologin
    operator:x:::operator:/rooot:/sbin/nologin
    roooot:x:::/rooooot:/bin/bash
    
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

    [0-9]表示任意的数字。这里你也可以写成[a-zA-Z]甚至[0-9a-zA-Z]

    [root@localhost ~]# sed 's/[a-zA-Z]//g' test.txt
    ::0:0:/://
    ::11:0::/://
    ::11:0::/://
    ::0:0:/://
    1111111111111111111111111111111

    6.调换两个字符串的位置

    [root@localhost ~]# sed 's/(rot)(.*)(bash)/321/' test.txt
    bash:x:0:0:/rot:/bin/rot
    operator:x:11:0:operator:/root:/sbin/nologin
    operator:x:11:0:operator:/rooot:/sbin/nologin
    roooot:x:0:0:/rooooot:/bin/bash
    1111111111111111111111111111111
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

    这个就需要解释一下了,上例中用 () 把所想要替换的字符括起来成为一个整体,因为括号在sed中属于特殊符号,所以需要在前面加脱意字符 ‘’, 替换时则写成 ‘1’, ‘‘2’, ‘‘3’ 的形式。除了调换两个字符串的位置外,阿铭还常常用到在某一行前或者后增加指定内容。

    [root@localhost ~]# sed 's/^.*$/123&/' test.txt
    123rot:x:0:0:/rot:/bin/bash
    123operator:x:11:0:operator:/root:/sbin/nologin
    123operator:x:11:0:operator:/rooot:/sbin/nologin
    123roooot:x:0:0:/rooooot:/bin/bash
    1231111111111111111111111111111111
    123aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

    7.直接修改文件的内容

    [root@localhost ~]# sed -i 's/ot/to/g' test.txt
    [root@localhost ~]# cat test.txt
    rto:x:0:0:/rto:/bin/bash
    operator:x:11:0:operator:/roto:/sbin/nologin
    operator:x:11:0:operator:/rooto:/sbin/nologin
    roooto:x:0:0:/rooooto:/bin/bash
    1111111111111111111111111111111
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

    这样就可以直接更改test.txt文件中的内容了。由于这个命令可以直接把文件修改,所以在修改前最好先复制一下文件以免改错。

    sed常用到的也就上面这些了,只要你多加练习就能熟悉它了。

    习题

    1. 把/etc/passwd 复制到/root/test.txt,用sed打印所有行
    2. 打印test.txt的3到10行
    3. 打印test.txt 中包含 ‘root’ 的行
    4. 删除test.txt 的15行以及以后所有行
    5. 删除test.txt中包含 ‘bash’ 的行
    6. 替换test.txt 中 ‘root’ 为 ‘toor’
    7. 替换test.txt中 ‘/sbin/nologin’ 为 ‘/bin/login’
    8. 删除test.txt中5到10行中所有的数字
    9. 删除test.txt 中所有特殊字符(除了数字以及大小写字母)
    10. 把test.txt中第一个单词和最后一个单词调换位置
    11. 把test.txt中出现的第一个数字和最后一个单词替换位置
    12. 把test.txt 中第一个数字移动到行末尾
    13. 在test.txt 20行到末行最前面加 ‘aaa:’

    sed习题答案

    1.  /bin/cp /etc/passwd  /root/test.txt ;  sed -n '1,$'p test.txt
    2.  sed -n '3,10'p test.txt
    3.  sed -n '/root/'p test.txt
    4.  sed '15,$'d  test.txt
    5.  sed '/bash/'d test.txt
    6.  sed 's/root/toor/g' test.txt
    7.  sed 's#sbin/nologin#bin/login#g' test.txt
    8.  sed '5,10s/[0-9]//g' test.txt
    9.  sed 's/[^0-9a-zA-Z]//g' test.txt
    10.  sed 's/(^[a-zA-Z][a-zA-Z]*)([^a-zA-Z].*)([^a-zA-Z])([a-zA-Z][a-zA-Z]*$)/4231/' test.txt
    11.  sed 's#([^0-9][^0-9]*)([0-9][0-9]*)([^0-9].*)([^a-zA-Z])([a-zA-Z][a-zA-Z]*$)#15342#' test.txt
    12.  sed 's#([^0-9][^0-9]*)([0-9][0-9]*)([^0-9].*$)#132#' test.txt
    13.  sed '20,$s/^.*$/aaa:&/' test.txt
    坚持!
  • 相关阅读:
    Mysql安装(msi版的安装)
    001 springBoot的数据库操作
    使用IDEA搭建spring
    spring简介
    rabbitMQ的安装(Windows下)
    mysql过滤数据
    mysql查询数据
    均值滤波,中值滤波,最大最小值滤波
    图像运动去模糊(Motion Deblurring)代码
    数字图像处理,图像锐化算法的C++实现
  • 原文地址:https://www.cnblogs.com/doubilaile/p/7877585.html
Copyright © 2011-2022 走看看