zoukankan      html  css  js  c++  java
  • 三剑客之sed

    三剑客 sed

    功能说明:

    sed  stream editor  增删改查 过滤 取行

    语法格式:

    sed [option] [sed -commads] [iput-file]

    sed 选项      sed命令        输出文件

    input-file 可以是文本 也可以是标准输入 例如cat test.txt

    [option]

    -n 取消默认输出)

    -i 修改文件 sed -i.ori 's#NB#benjamin#g' person.txt 给原文件备份生成 person.txt.ori

    -f 后接sed脚本文件名

    -r 使用扩展正则表达式

    -e 执行多条sed命令

    [sed-commands]

    a 追加

    i 插入

    d 删除

    c 替换指定的行

    s 替换指定字符串 g 命令的替换标志-全局替换标志

    p 输出指定内容,与-n参数一起使用  -n取消默认输出

    l  输出不可见字符

    w 另存文件

    [指定执行的地址范围]

    n1[,n2] {sed-commands}

    10{sed-commands}

    10,20{sed-commands}

    10,+20{sed-commands} #10-30行执行sed命令

    1~2{sed-commands} #1行开始 2为步长 匹配执行sed命令

    [root@linux-node1 ~]# seq 10| sed -n '1~2p'

    1

    3

    5

    7

    9

    10,${sed-commands} #10到最后一行 执行sed命令

    /benjamin/ {sed-commands}

    /benjamin/,/Alex/{sed-commands}

    /benjamin/.$

    10,/Alex/{sed-commands}

    /Alex/,10{sed-commands}

    /benjamin/,+20{sed-commands}

    创建一个person.txt文本

    cat person.txt

    101,benjamin,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    1. 增删改查

    1.1、增 a  i

    单行增加:

    #在第二行后面追加106,dandan,CSO

    [root@linux-node1 ~]# sed '2a 106,dandan,CSO' person.txt  

    101,benjamin,CEO

    102,xiaozhang,CTO

    106,dandan,CSO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    #在第二行前面追加106,dandan,CSO

    [root@linux-node1 ~]# sed '2i 106,dandan,CSO' person.txt   

    101,benjamin,CEO

    106,dandan,CSO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    多行增加

    #第二行后面增加两行

    [root@linux-node1 ~]# sed '2a 106,dandan,CSO 107,binbin.CSO' person.txt

    101,benjamin,CEO

    102,xiaozhang,CTO

    106,dandan,CSO

    107,binbin.CSO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    #或者使用下面的方法是一样的

    [root@linux-node1 ~]# sed '2a 106,dandan,CSO

    > 107,binbin.CSO' person.txt

    101,benjamin,CEO

    102,xiaozhang,CTO

    106,dandan,CSO

    107,binbin.CSO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    企业案例1ssh优化 修改默认端口号

    [root@linux-node1 ~]# sed -ir '13i Port 52113 PermitRootLogin no PermitEmptyPasswords no UseDNS no GSSAPIAuthentication no' /etc/ssh/sshd_config

    1.2、删  d

    #全删

    [root@linux-node1 ~]# sed 'd' person.txt

    #删除第2

    [root@linux-node1 ~]# sed '2d' person.txt

    101,benjamin,CEO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    #删除2-5

    [root@linux-node1 ~]# sed '2,5d' person.txt

    101,benjamin,CEO

    #删除2-最后一行

    [root@linux-node1 ~]# sed '2,$d' person.txt

    101,benjamin,CEO

    #删除基数行

    [root@linux-node1 ~]sed '1~2d' person.txt

    102,xiaozhang,CTO

    104,yy,CFO

    #删除匹配到benjamin的行

    [root@linux-node1 ~]# sed '/benjamin/d' person.txt    

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    企业案例2:打印文件内容但不包括benjamin

    sed  ‘/oldboy/d’ person.txt

    grep -v benjamin persong.txt

    1.3、改  c  s

    1.3.1 按行替换

    # 替换第二行(不常用,一般替换部分文本不会整行替换,了解即可)

    [root@linux-node1 ~]# sed '2c 106,diandian,sco' person.txt

    101,benjamin,CEO

    106,diandian,sco

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    1.3.2 指定字符串替换

    # 匹配benjamin字符串替换为 NB (不加i保存在内存里也可称为模式空间)

    [root@linux-node1 ~]# sed s#benjamin#NB#g person.txt

    101,NB,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    #去配置文件的注释

    [root@linux-node1 ~] grep -v '^#' /etc/inittab|grep -v $' '

    id:3:initdefault:

    企业案例3:永久关闭selinux

    [root@linux-node1 ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

    1.3.3、变量替换

    cat zimu.txt

    a

    b

    a

    [root@linux-node1 ~]# x=a

    [root@linux-node1 ~]# y=b

    [root@linux-node1 ~]# echo $x,$y

    a,b

    [root@linux-node1 ~]# sed "s#$x#$y#g" zimu.txt   #注意这里是双引号  变量的引用     

    b

    b

    b

    #不用双引号也可以替换:

    [root@linux-node1 ~]# sed 's#'$x'#'$y'#' zimu.txt

    b

    b

    b

    [root@linux-node1 ~]# eval sed 's#$x#$y#g' zimu.txt                

    b

    b

    b

    1.3.4、分组替换 () 1

    说明:^.*am空格 匹配以任意字符接am开头的字符对应的是 I am空格

         ([a-z].*) 匹配任意小写字母  对应oldboy

         空格tea.*$ 匹配任意以空格tea字符 对应的是 空格tea

         1 正向引用 引用的是括号里匹配的字符串 1代表第一个括号内匹配的内容

    [root@linux-node1 ~]# echo I am oldboy teacher. |sed ' s#^.*am ([a-z].*) tea.*$#1#g'

    oldboy

    [root@linux-node1 ~]# echo I am oldboy teacher. |sed ' s#I (.*) ([a-z].*) tea.*$#1 2#g'

    am oldboy

    企业案例4:系统开机启动项优化

    sed -r 代表 扩展正则 使用了就不用进行转义类似 grep -E

    chkconfig --list|grep 3:on|grep -Ev "sshd|network|rsyslog|sysstat|crond"|awk '{print $1}'|sed -r 's#(.*)#chkconfig 1 off#g'|bash

    1.3.5、 特殊符号& 代表被替换的内容

    #替换13C字符 替换为  --C--

    [root@linux-node1 ~]# sed '1,3s#C#--&--#g' person.txt

    101,benjamin,--C--EO

    102,xiaozhang,--C--TO

    103,Alex,--C--OO

    104,yy,CFO

    105,feixue,CI

    #不加 -r 不起效果,

    [root@linux-node1 ~]# sed '1,3s#(.*)#--&--#g' person.txt

    101,benjamin,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    #-r 启用扩展正则

    [root@linux-node1 ~]# sed -r '1,3s#(.*)#--&--#g' person.txt   

    --101,benjamin,CEO--

    --102,xiaozhang,CTO--

    --103,Alex,COO--

    104,yy,CFO

    105,feixue,CIO

    [root@linux-node1 data]# sed -r '1,3s#(.*),(.*),(.*)#--&--#g' ~/person.txt

    --101,benjamin,CEO--

    --102,xiaozhang,CTO--

    --103,Alex,COO--

    104,yy,CFO

    105,feixue,CIO

    企业案例5:批量重命名文件   去掉文件中_finished

    [root@linux-node1 data]# for i in {1..5};do touch stu_10299_${i}_finished.jpg;done
    [root@linux-node1 data]# ls
    etcd stu_10299_1_finished.jpg stu_10299_3_finished.jpg stu_10299_5_finished.jpg
    php-fpm.log stu_10299_2_finished.jpg stu_10299_4_finished.jpg

    [root@linux-node1 data] ls *jpg|sed -r 's#(.*)_finished.*#mv & 1.jpg#g'|bash

    [root@linux-node1 data]# ls
    etcd stu_10299_1.jpg stu_10299_3.jpg stu_10299_5.jpg
    php-fpm.log stu_10299_2.jpg stu_10299_4.jpg

    1.4、查 p 正常和 -n 选项

    一起使用 不加 -n 除了输出指定查询行还会默认输出所有行

    [root@linux-node1 ~]# sed '2p' person.txt

    101,benjamin,CEO

    102,xiaozhang,CTO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    [root@linux-node1 ~]# sed -n '2p' person.txt

    102,xiaozhang,CTO

    #p前面不加数字默认输出所有行

    [root@linux-node1 ~]# sed -n 'p' person.txt   #不加n所有输出2

    101,benjamin,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    [root@linux-node1 ~]# sed '' person.txt  #什么不加相当于cat

    101,benjamin,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    [root@linux-node1 ~]# sed -n '/CTO/,5 p' person.txt

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    2. 修改文件

     #sed自带备份功能

    [root@linux-node1 ~]# sed -i.ori 's#benjamin#NB#g' person.txt

    [root@linux-node1 ~]# cat person.txt

    101,NB,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    [root@linux-node1 ~]# cat person.txt.ori

    101,benjamin,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    #还原文件

    [root@linux-node1 ~]# cat person.txt.ori > person.txt

    [root@linux-node1 ~]# cat person.txt

    101,benjamin,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    3.另存文件

    # w命令可以另存文件

    [root@linux-node1 ~]# sed 'w output.txt' person.txt

    101,benjamin,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    [root@linux-node1 ~]# cat output.txt

    101,benjamin,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    [root@linux-node1 ~]# sed 's#benjamin#NB#g' 'w output.txt'  person.txt

    101,NB,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    #保存指定行

    [root@linux-node1 ~]# sed '5w output.txt' person.txt

    101,benjamin,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    [root@linux-node1 ~]# cat output.txt

    105,feixue,CIO

    4.替换命令S详解

    sed '[address-range|pattern-range] s#original-string#replacement-string#[]'

    sed ' [地址范围]|[模式范围] s#[被替换的字符串]#[替换后的字符串]#[替换标志]'

    替换标志: g全局标志 数字标志1,2,3,。。。 打印p w 忽略大小写i 执行命令标志 e

    [root@linux-node1 ~]# cat num.txt

    1 1 1 1 1

    1 1 1 1 1

    1 1 1 1 1

    1 1 1 1 1

    4.1g标志

    #不加g标志 缺省默认为只修改指定行的第一列

    [root@linux-node1 ~]# sed '2s#1#0#' num.txt

    1 1 1 1 1

    0 1 1 1 1

    1 1 1 1 1

    1 1 1 1 1

    #g标志 指定行的所有列

    [root@linux-node1 ~]# sed '2s#1#0#g' num.txt

    1 1 1 1 1

    0 0 0 0 0

    1 1 1 1 1

    1 1 1 1 1

    4.2、数字标志

    #ng 标志 指定行的n列开始全部修改

    [root@linux-node1 ~]# sed '2s#1#0#2g' num.txt

    1 1 1 1 1

    1 0 0 0 0

    1 1 1 1 1

    1 1 1 1 1

    #第二行和第三行分别修改

    [root@linux-node1 ~]# sed '2,3s#1#0#2g' num.txt

    1 1 1 1 1

    1 0 0 0 0

    1 0 0 0 0

    1 1 1 1 1

    #缺省指定行的情况下默认 是所有行操作

    [root@linux-node1 ~]# sed 's#1#0#2g' num.txt    

    1 0 0 0 0

    1 0 0 0 0

    1 0 0 0 0

    1 0 0 0 0

    #不加g 指定标志为n  即修改指定行的第n

    [root@linux-node1 ~]# sed 's#1#0#2' num.txt  

    1 0 1 1 1

    1 0 1 1 1

    1 0 1 1 1

    [root@linux-node1 ~]# sed '2s#1#0#2' num.txt

    1 1 1 1 1

    1 0 1 1 1

    1 1 1 1 1

    1 1 1 1 1

    1 0 1 1 1

    4.3p打印标志

    # p标志匹配输出 -n取消默认输出

    [root@linux-node1 ~]# sed -n 's#benjamin#NB#p' person.txt

    101,NB,CEO

    4.4w 写入标志

    [root@linux-node1 ~]# sed 's#benjamin#NB#w out.txt' person.txt

    101,NB,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    [root@linux-node1 ~]# cat out.txt

    101,NB,CEO

    # 如果加个; 先执行替换然后将默认输出 输出到指定文件内

    [root@linux-node1 ~]# sed 's#benjamin#NB#;w out.txt' person.txt

    101,NB,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    [root@linux-node1 ~]# cat out.txt

    101,NB,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    4.5、 忽略大小写 标志i  grep -i也是忽略大小写

    #不加 i 不能匹配Alex

    [root@linux-node1 ~]# sed 's#alex#NB#g' person.txt

    101,benjamin,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    [root@linux-node1 ~]# sed 's#alex#NB#ig' person.txt

    101,benjamin,CEO

    102,xiaozhang,CTO

    103,NB,COO

    104,yy,CFO

    105,feixue,CIO

    [root@linux-node1 ~]# grep -i alex person.txt

    103,Alex,COO

    4.6、执行标志e

    [root@linux-node1 ~]# cat file.txt

    /etc/my.cnf

    /etc/passwd

    #注意命令结束后面接空格 不接空格会报错

    [root@linux-node1 ~]# sed 's#^#ls -lh #e' file.txt

    -rw-r--r-- 1 root root 251 127 2017 /etc/my.cnf

    -rw-r--r-- 1 root root 1.8K 117 01:53 /etc/passwd

    企业案例4:系统开机启动项优化

    #使用bash执行

    chkconfig --list|grep 3:on|grep -Ev "sshd|network|rsyslog|sysstat|crond"|

    awk '{print $1}'|sed -r 's#(.*)#chkconfig 1 off#g'|bash

    chkconfig --list|grep 3:on|grep -Ev "sshd|network|rsyslog|sysstat|crond"|

    awk '{print $1}'|sed -r 's#(.*)#chkconfig 1 off#e’

    4.7其他替换标志

    l 在替换字符中使用l标志时,他会把紧跟在其后面的字符当作小写字符来处理

    [root@linux-node1 ~]# sed -n 's#benjamin#BENlJAMIN#p' person.txt

    101,BENjAMIN,CEO

    L 在替换字符中使用L标志时,他会把后面所有的字符当作小写字符来处理

    [root@linux-node1 ~]# sed -n 's#benjamin#BENLJAMIN#p' person.txt

    101,BENjamin,CEO

    u 在替换字符中使用l标志时,他会把紧跟在其后面的字符当作大写字符来处理

    [root@linux-node1 ~]# sed -n 's#benjamin#benujamin#p' person.txt               

    101,benJamin,CEO

    U 在替换字符中使用L标志时,他会把后面所有的字符当作大写字符来处理

    [root@linux-node1 ~]# sed -n 's#benjamin#benUjamin#p' person.txt

    101,benJAMIN,CEO

    E 需要和UL一起使用,它将关闭U或者L的功能。

    [root@linux-node1 ~]# sed -r 's#(.*),(.*),(.*)#L3,E1,U2#g' person.txt

    ceo,101,BENJAMIN

    cto,102,XIAOZHANG

    coo,103,ALEX

    cfo,104,YY

    cio,105,FEIXUE

    [root@linux-node1 ~]# cat person.txt

    101,benjamin,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    5. 特殊符号= 获取取行号

    [root@linux-node1 ~]# sed '=' person.txt

    1

    101,benjamin,CEO

    2

    102,xiaozhang,CTO

    3

    103,Alex,COO

    4

    104,yy,CFO

    5

    105,feixue,CIO

    [root@linux-node1 ~]# sed '1,3=' person.txt

    1

    101,benjamin,CEO

    2

    102,xiaozhang,CTO

    3

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    [root@linux-node1 ~]# sed '/benjamin/=' person.txt   

    1

    101,benjamin,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    #cat -n 也是可以显示

    [root@linux-node1 ~]# cat -n person.txt

         1  101,benjamin,CEO

         2  102,xiaozhang,CTO

         3  103,Alex,COO

         4  104,yy,CFO

         5  105,feixue,CIO

    #N 不清空当前模式空间,然后读入下一行 以 分隔两行

    [root@linux-node1 ~]# sed '=' person.txt|sed 'N;s# # #'

    1 101,benjamin,CEO

    2 102,xiaozhang,CTO

    3 103,Alex,COO

    4 104,yy,CFO

    5 105,feixue,CIO

    6. 一条sed语句执行多条sed命令

    需求:删除第三行到末尾的数字,并把10替换为01

    #方法一

    [root@linux-node1 ~]# sed '3,$d' person.txt

    101,benjamin,CEO

    102,xiaozhang,CTO

    [root@linux-node1 ~]# sed '3,$d' person.txt|sed 's#10#01#g'

    011,benjamin,CEO

    012,xiaozhang,CTO

    #方法二

    [root@linux-node1 ~]# sed -e '3,$d' -e 's#10#01#g'  person.txt

    011,benjamin,CEO

    012,xiaozhang,CTO

    #方法三(推荐方法)

    [root@linux-node1 ~]# sed -e '3,$d;s#10#01#g'  person.txt      

    011,benjamin,CEO

    012,xiaozhang,CTO

    #方法四

    [root@linux-node1 ~]# cat person.sed

    3,$d

    s#10#01#g

    [root@linux-node1 ~]# sed -f person.sed person.txt

    011,benjamin,CEO

    012,xiaozhang,CTO

    企业案例6:一个文件100行、把5,35,70行单独拿出来  

    [root@linux-node1 ~]# sed -n '5p;35p;70p' /etc/services

    # IANA services version: last updated 2009-11-10

    qotd            17/tcp          quote

    whois++         63/udp

    7. 特殊符号{}用法

      #不加大括号,p执行2-4行,但是=号会执行所有行

    [root@linux-node1 ~]# sed -n '2,4p;=' person.txt

    1

    102,xiaozhang,CTO

    2

    103,Alex,COO

    3

    104,yy,CFO

    4

    5

    #大括号将所要执行的命令括起来,然后只执行选定的行号,类似乘法分配律

    [root@linux-node1 ~]# sed -n '2,4{p;=}' person.txt

    102,xiaozhang,CTO

    2

    103,Alex,COO

    3

    104,yy,CFO

    4

    [root@linux-node1 ~]# sed -n '2,4p;2,4=' person.txt     

    102,xiaozhang,CTO

    2

    103,Alex,COO

    3

    104,yy,CFO

    4

    8. 打印不可见字符

    [root@linux-node1 ~]# sed -n 'l' person.txt

    101,benjamin,CEO$

    102,xiaozhang,CTO$

    103,Alex,COO$

    104,yy,CFO$

    105,feixue,CIO$

    #加个tab键 显示

    [root@linux-node1 ~]# sed -n 'l' person.txt

    101,benjamin,CEO $

    102,xiaozhang,CTO$

    103,Alex,COO$

    104,yy,CFO$

    105,feixue,CIO$

    9. 转换字符y命令

    [root@linux-node1 ~]# tr 'abc' 'ABC' < person.txt

    101,BenjAmin,CEO

    102,xiAozhAng,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    [root@linux-node1 ~]# sed 'y#abc#ABC#' person.txt

    101,BenjAmin,CEO

    102,xiAozhAng,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    10. 退出sed  q命令(quit

    [root@linux-node1 ~]# sed 'q' person.txt

    101,benjamin,CEO

    [root@linux-node1 ~]# sed '3q' person.txt

    101,benjamin,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    #不可以范围匹配

    [root@linux-node1 ~]# sed '3,4q' person.txt

    sed: -e expression #1, char 4: command only uses one address

    [root@linux-node1 ~]# sed '3q;p' person.txt    

    101,benjamin,CEO

    101,benjamin,CEO

    102,xiaozhang,CTO

    102,xiaozhang,CTO

    103,Alex,COO

    11. 从文件读取数据r命令(read

    [root@linux-node1 ~]# sed 'r num.txt' person.txt

    101,benjamin,CEO

    1 1 1 1 1

    1 1 1 1 1

    1 1 1 1 1

    1 1 1 1 1

    102,xiaozhang,CTO

    1 1 1 1 1

    1 1 1 1 1

    1 1 1 1 1

    1 1 1 1 1

    103,Alex,COO

    1 1 1 1 1

    1 1 1 1 1

    1 1 1 1 1

    1 1 1 1 1

    104,yy,CFO

    1 1 1 1 1

    1 1 1 1 1

    1 1 1 1 1

    1 1 1 1 1

    105,feixue,CIO

    1 1 1 1 1

    1 1 1 1 1

    1 1 1 1 1

    1 1 1 1 1

    #从最后一行开始读取文件追加,相当于合并文件 cat num.txt >> person.txt

    [root@linux-node1 ~]# sed '$r num.txt' person.txt

    101,benjamin,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    1 1 1 1 1

    1 1 1 1 1

    1 1 1 1 1

    1 1 1 1 1

    1. 模式空间和保持空间

    模式空间(pattern space):是sed软件从文件读取一行文本然后存入的缓冲区,然后命令操作模式空间的内容。

    保持空间(hold space):是sed软件另外一个缓冲区,用来存放临时数据。Sed可以交换保持空间和模式空间的数据,但是不能在保持空间上执行普通的sed命令。

    初始情况,模式空间和保持空间都是没有内容的。每次循环读取数据的过程中,模式空间内容都会被清空写入新的内容,但保持空间的内容保持不变,不会在循环中被删除,除非sed命令操作保持空间。

    模式空间命令

    n :清空当前模式空间,然后读入下一行

    [root@linux-node1 ~]# sed -n 'p' person.txt  

    101,benjamin,CEO

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    #先读取一行n命令清空后在执行p命令 完成一次循环

    [root@linux-node1 ~]# sed -n 'n;p' person.txt

    102,xiaozhang,CTO

    104,yy,CFO

    N :不清空当前模式空间,然后读入下一行,以 分隔两行

    [root@linux-node1 ~]# sed '=' person.txt|sed 's# # #g'

    1

    101,benjamin,CEO

    2

    102,xiaozhang,CTO

    3

    103,Alex,COO

    4

    104,yy,CFO

    5

    105,feixue,CIO

    ==》模式空间的内容

    1 101,benjamin,CEO

    2 102,xiaozhang,CTO

    3 103,Alex,COO

    4 104,yy,CFO

    5 105,feixue,CIO

    ==>s# # #

    [root@linux-node1 ~]# sed  '=' person.txt|sed 'N;s# # #'

    1 101,benjamin,CEO

    2 102,xiaozhang,CTO

    3 103,Alex,COO

    4 104,yy,CFO

    5 105,feixue,CIO

    企业案例7:密码文件test.txt 其中账号密码换行输出 现在要求  每行 账号=密码 这样 的格式输出 stu10301=21324cc1

    [root@linux-node1 ~]#cat test.txt

    stu10301

    21324cc1

    stu10302

    ergtge23

    stu10303

    dfgeg22d

    stu10304

    dfgerw23

    stu10305

    sd3232df

    stu10306

    erwe234e

    stu10307

    werwe23d

    stu10308

    efwe331e

    [root@linux-node1 ~]# sed 'N;s# #=#' test.txt

    stu10301=21324cc1

    stu10302=ergtge23

    stu10303=dfgeg22d

    stu10304=dfgerw23

    stu10305=sd3232df

    stu10306=erwe234e

    stu10307=werwe23d

    stu10308=efwe331e

    12.操作多个文件

    [root@linux-node1 ~]# sed 'N;s# #=#' test.txt person.txt

    stu10301=21324cc1

    stu10302=ergtge23

    stu10303=dfgeg22d

    stu10304=dfgerw23

    stu10305=sd3232df

    stu10306=erwe234e

    stu10307=werwe23d

    stu10308=efwe331e

    101,benjamin,CEO=102,xiaozhang,CTO

    103,Alex,COO=104,yy,CFO

    105,feixue,CIO

    1. 模拟其他命令

    #模拟cat命令

    sed -n ‘p’ person.txt

    sed ‘n’ person.txt

    sed ‘N’ person.txt

    sed ‘s# # #’ person.txt

    #模拟grep命令

    [root@linux-node1 ~]# sed -n '/ben/p' person.txt

    101,benjamin,CEO

    #模拟 grep -v 命令

    [root@linux-node1 ~]# sed -n '/ben/ !p' person.txt

    102,xiaozhang,CTO

    103,Alex,COO

    104,yy,CFO

    105,feixue,CIO

    # 模拟head命令

    [root@linux-node1 ~]# sed -n '1,2p' person.txt  

    101,benjamin,CEO

    102,xiaozhang,CTO

    [root@linux-node1 ~]# sed  '3,$d' person.txt  

    101,benjamin,CEO

    102,xiaozhang,CTO

    [root@linux-node1 ~]# sed  '2q' person.txt    

    101,benjamin,CEO

    102,xiaozhang,CTO

    #模拟wc命令

    [root@linux-node1 ~]# sed -n '$=' person.txt

    5

  • 相关阅读:
    《算法导论》读书笔记(五)
    《算法导论》读书笔记(四)
    《算法导论》读书笔记(三)
    《算法导论》读书笔记(二)
    《算法导论》读书笔记(一)
    Posix消息队列
    管道和FIFO
    linux内核数据结构之kfifo
    linux内核数据结构之链表
    Mybatis XML 映射配置文件 -- 熟悉配置
  • 原文地址:https://www.cnblogs.com/benjamin77/p/7866050.html
Copyright © 2011-2022 走看看