zoukankan      html  css  js  c++  java
  • 【文本处理命令】之sed命令详解

    sed行处理命令详解

    一、简介

      sed命令是一种在线编辑器。一个面向字符流的非交互式编辑器,也就是说sed不允许用户与它进行交互操作。sed是按行来处理文本内容的,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。在shell中,使用sed来批量修改文本内容是非常方便的。

    参考网址:https://blog.51cto.com/13517084/2069074

    二、sed用法

    sed命令能进行增删改查操作

    格式:

    sed [OPTION]... {script-only-if-no-other-script} [input-file]...
    即
    sed [选项] [动作] [输出文件]

    选项:

    -n :使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN 的数据一般都会被列出到终端上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
    -e :直接在命令列模式上进行 sed 的动作编辑;
    -f :直接将 sed 的动作写在一个文件内, -f filename 则可以运行 filename 内的 sed 动作;
    -r :sed 的动作支持的是延伸型正规表示法的语法。(默认是基础正规表示法语法)
    -i :直接修改读取的文件内容,而不是输出到终端。禁止与-n一起使用,会将原文件清空;
    动作说明: [n1[,n2]]function
    n1, n2 :不见得会存在,一般代表[选择进行动作的行数],举例来说,如果我的动作是需要在 10 到 20 行之间进行的,则[ 10,20[动作行为] ]

    function:

    a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(在下一行添加)
    c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
    d :删除, d 后面通常不接任何东西;
    i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(在上一行插入)。
    p :列印,即将某个选择的数据打印出来。通常 p 与参数 sed -n 一起使用
    s :取代,可以直接进行取代的工作!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g ,将1-20行包含old的全部替换为new

    正则中的元字符:

    $ 表示行尾 
    ^ 表示行首
    [a-z0-9]表示字符范围
    [^]表示除了字符集中的字符以外的字符
    & 正则表达式所匹配的内容 sed的正则中 () 和 {m,n} 需要转义 . 表示任意字符
    * 表示零个或者多个 + 一次或多次   ? 零次或一次 | 表示或语法
    字符串表示正则匹配单词

    不显示文件中的空行

    # !在sed、awk find中表示取反
    grep -v '^$' /tmp/passwd
    # 去掉空行和以#开头的行
    cat /tmp/passwd |grep -v '^$' | grep -v '^#' sed
    '/^$/d' /tmp/passwd
    # 匹配空格,不显示,!表示不显示有空格的行 awk
    '!/^$/' /tmp/passed sed -n '/^$/!p' /tmp/passwd

    三、sed实例

    3.1.搜索p

    准备一个txt文件

    [root@VM_0_10_centos shellScript]# cat txt.txt 
    2 this is a test
    3 Are you like awk
    This's a test
    10 There are orange,apple,mongo

    1)显示txt文件第3行的内容

    # 加-n和不加的区别(不加-n会在原文件下面打印需要打印的行。加-n只显示需要打印的行)
    [root@VM_0_10_centos shellScript]# sed '3p' txt.txt 
    2 this is a test
    3 Are you like awk
    This's a test
    This's a test
    10 There are orange,apple,mongo
    [root@VM_0_10_centos shellScript]# sed -n '3p' txt.txt 
    This's a test

    2)连续显示多行信息输出到屏幕

    [root@VM_0_10_centos shellScript]# sed -n '1,3p' txt.txt 
    2 this is a test
    3 Are you like awk
    This's a test

    3)显示包含"s"的行。'//':表示过滤内容,可以匹配正则表达式

    [root@VM_0_10_centos shellScript]# sed -n '/s/p' txt.txt 
    2 this is a test
    This's a test

    PS:需匹配多个条件,使用 " , " 分隔;同一行多个命令使用";"分隔

    [root@VM_0_10_centos shellScript]# sed -n '1p;2p' txt.txt 
    2 this is a test
    3 Are you like awk

    搜索并执行命令

    1)搜索passwd文件中带root的字符,将其shell替换为/bin/bashsed

    # p直接接在后面是打印出替换的这一行;p前面加上;p会打印出修改的行及包含root的行
    [root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed -n '/root/{s/bash/bashsed/p}'
         1    root:x:0:0:root:/root:/bin/bashsed
    [root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed -n '/root/{s/bash/bashsed/;p}'
         1    root:x:0:0:root:/root:/bin/bashsed
        10    operator:x:11:0:operator:/root:/sbin/nologin

    2)匹配包含bash字符,将其替换为bashsed,并退出

    [root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed -n '/bash/{s/bash/bashsed/;p}'
         1    root:x:0:0:root:/root:/bin/bashsed
        29    apache:x:1000:1002::/home/apache:/bin/bashsed
        35    usertest1:x:1006:1008::/home/usertest1:/bin/bashsed
    [root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed -n '/bash/{s/bash/bashsed/p}'
         1    root:x:0:0:root:/root:/bin/bashsed
        29    apache:x:1000:1002::/home/apache:/bin/bashsed
        35    usertest1:x:1006:1008::/home/usertest1:/bin/bashsed
    [root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed -n '/bash/{s/bash/bashsed/;p;q}'
         1    root:x:0:0:root:/root:/bin/bashsed

    搜索并替换

    格式:

    sed 's/要被取代的字串/新的字串/g'

    1)打印出ip信息。先使用ifconfig eth0查看网卡,使用grep -w过滤出只包含inet的行,使用sed命令用正则表达式匹配截取掉ip开头和结尾的数据

    [root@VM_0_10_centos shellScript]# ifconfig eth0 | grep -w inet | sed 's/^.*inet//g' | sed 's/netmask.*$//g'
     172.16.0.10

    2)对所有以小写或大写a-zA-Z范围开头的行,执行替换操作

    [root@VM_0_10_centos shellScript]# sed '/^[a-zA-Z]/s/r/u/' /tmp/passwd 
    uoot:x:0:0:root:/root:/bin/bash
    [root@VM_0_10_centos shellScript]# sed '/^[a-zA-Z]/s/r/u/g' /tmp/passwd 
    uoot:x:0:0:uoot:/uoot:/bin/bash
    [root@VM_0_10_centos shellScript]# sed '/^[a-zA-Z]/{s/r/u/}' /tmp/passwd 
    uoot:x:0:0:root:/root:/bin/bash
    [root@VM_0_10_centos shellScript]# sed '/^[a-zA-Z]/{s/r/u/g}' /tmp/passwd 
    uoot:x:0:0:uoot:/uoot:/bin/bash

    3)在passwd文件最好添加内容

    # 将EOF输出的内容赋给cat命令输入到passwd文件中(将最后一个命令作为前一个命令的输入)
    [root@VM_0_10_centos shellScript]# cat >> /tmp/passwd << EOF 
    > 1
    > 2
    > 3
    > EOF

    多点编辑 -e

    [root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed -e '3,$d' -e 's/bash/bashsed/g'
         1    root:x:0:0:root:/root:/bin/bashsed
         2    bin:x:1:1:bin:/bin:/sbin/nologin

    3.2.增加  a或i

    使用/etc/passed文件操作,先备份好。使用备份的操作

    以行为单位新增或删除

    1)将 /etc/passwd 的内容列出并且列出行号,同时,将第 2~5 行删除!

    [root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '2,5d'
         1    root:x:0:0:root:/root:/bin/bash
         6    sync:x:5:0:sync:/sbin:/bin/sync
         7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
         8    halt:x:7:0:halt:/sbin:/sbin/halt
         9    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
        10    operator:x:11:0:operator:/root:/sbin/nologin
    # 如果只删除第二行,只需要'2d'即可

    2)删除第3行到最后一行

    [root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '3,$d'
         1    root:x:0:0:root:/root:/bin/bash
         2    bin:x:1:1:bin:/bin:/sbin/nologin

    3)在第2行后面添加一行,在第2行前面添加一行

    [root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '2a hello sed'
    1    root:x:0:0:root:/root:/bin/bash
    2    bin:x:1:1:bin:/bin:/sbin/nologin
    hello sed
    3    daemon:x:2:2:daemon:/sbin:/sbin/nologin

     [root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '2i before sed'
     1 root:x:0:0:root:/root:/bin/bash
     before sed
     2 bin:x:1:1:bin:/bin:/sbin/nologin

    如果是要添加两行或以上。需在添加的信息后面接 " " ,然后回车,输入要添加的信息

    [root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '2a add1
    > add2'
         1    root:x:0:0:root:/root:/bin/bash
         2    bin:x:1:1:bin:/bin:/sbin/nologin
    add1
    add2
         3    daemon:x:2:2:daemon:/sbin:/sbin/nologin

    3.3.替换  c

    以行为单位的替换

     1)将2~5行的内容替换为change

    [root@VM_0_10_centos shellScript]# nl /tmp/passwd | sed '2,5c change'
         1    root:x:0:0:root:/root:/bin/bash
    change
         6    sync:x:5:0:sync:/sbin:/bin/sync

    3.4.直接修改原文件内容(危险动作)

    sed 可以直接修改文件的内容,不必使用管道命令或数据流重导向! 不过,由於这个动作会直接修改到原始的文件,所以请你千万不要随便拿系统配置来测试!

    1)先备份在修改文件内容

    [root@VM_0_10_centos shellScript]# sed -i.bak 's/[1-9]/yy/g' /tmp/passwd 
    [root@VM_0_10_centos shellScript]# ls /tmp/
    passwd
    passwd.bak
    或
    [root@VM_0_10_centos shellScript]# sed -i.bak 's#y#1234#g' /tmp/passwd

    2)利用 sed 将 regular_express.txt 内每一行结尾若为 . 则换成 !

    [root@www ~]# sed -i 's/.$/!/g' regular_express.txt

    3)利用 sed 直接在 regular_express.txt 最后一行加入『# This is a test』

    [root@www ~]# sed -i '$a # This is a test' regular_express.txt

    由於 $ 代表的是最后一行,而 a 的动作是新增,因此该文件最后新增『# This is a test』!

     3)变量替换,只能使用双引号识别

    [root@VM_0_10_centos shellScript]# x=1234
    [root@VM_0_10_centos shellScript]# y=new
    [root@VM_0_10_centos shellScript]# sed "s/$x/$y/g" /tmp/passwd

    3.5 后项引用

    1)将匹配的内容按规定格式进行输出

    [root@VM_0_10_centos shellScript]# sed -r 's/(.*)/<1>/' /tmp/passwd
    <12341234123412341234123412341234123412341234123412341234>
    <   n>

    匹配签名的内容进行格式输出

    [root@VM_0_10_centos shellScript]# echo '123456' | sed -r 's/(.*)/<1>/g'
    <123456>

    2)使用" & "匹配内容,进行格式输出

    [root@VM_0_10_centos shellScript]# sed -r 's/.*/<&>/g' /tmp/passwd
    [root@VM_0_10_centos shellScript]# echo '123456' | sed -r 's/(.*)/<1>/g'
    <123456>

     [root@VM_0_10_centos shellScript]# sed -r 's/./<&>/g' /tmp/passwd
     <1><2><3><4>

    3)命令拼接

    # 先新建jpg文件
    [root@VM_0_10_centos shellScript]# touch {old01,old02,old03,old04}.jpg
    [root@VM_0_10_centos shellScript]# ls *jpg
    old01.jpg  old02.jpg  old03.jpg  old04.jpg
    [root@VM_0_10_centos shellScript]# ls *.jpg |sed -r 's/(.*)jpg/mv & 1avg/g' 
    mv old01.jpg old01.avg
    mv old02.jpg old02.avg
    mv old03.jpg old03.avg
    mv old04.jpg old04.avg

    ls *.jpg|sed -r 's#(.*)jpg#mv & 1avi#g'|bash

    # rename [选项] 你要替换的内容 替换成什么 替换哪些文件
    rename -v avi jpg *.jpg

    PS:-r 与 -i 同时使用时,-r 参数必须放在前面,如 -ri。不能使用 -ir,这样会先将文件备份为文件.r,然后在替换文件

    四、案例

    txt文件:

    [root@rhel8 shell]# vim number.txt
    234 567 123 23
    24 56 146
    
    456
    789
    4567899 6778 0

    命令:

    # sort -n 按整数升序
    [root@rhel8 shell]# cat number.txt | sed 's/ /
    /g' | grep -v '^$'| sort -n
    0
    23
    24
    56
    123
    146
    234
    456
    567
    789
    6778
    4567899
    
    # sort -nr 降序
    [root@rhel8 shell]# cat number.txt | sed 's/ /
    /g' | grep -v '^$'| sort -nr
    4567899
    6778
    789
    567
    456
    234
    146
    123
    56
    24
    23
    0
    
    # 打印最大和最小数
    [root@rhel8 shell]# cat number.txt | sed 's/ /
    /g' | grep -v '^$'| sort -nr | sed -n '1p;$p'
    4567899
    0

    参考网址;https://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html

  • 相关阅读:
    [转载]选择比努力更重要
    [转载]weblogic中文文档——domain_config
    Java API 帮助文档中英文版下载
    [转载]BAT文件语法和技巧
    搜索字符串并高亮显示
    [转载]配置WebLogic Server集群
    Linux目录架构
    多文档多视图之间的切换过程和当文档多视图之间的切换过程
    SendMessage及WPRAME、LPARAME
    下MFC中对象、句柄、ID之间的区别.
  • 原文地址:https://www.cnblogs.com/HeiDi-BoKe/p/11685621.html
Copyright © 2011-2022 走看看