linux文本处理三剑客-sed

sed 是什么?

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

sed工作模型

图片.png

sed的基本语法

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

opotion 的选项:

-n 静默模式,不输出模式空间的内容

[root@localhost ~]#sed '1p' /etc/issue 
\S 
\S 
Kernel \r on an \m 
[root@localhost ~]#sed -n '1p' /etc/issue 
\S

-e script -e script  同时执行多个脚本

[root@localhost ~]#sed -e '1s/bash$/zsh/' -e 's/^root/toor/'  /etc/passwd  
toor:x:0:0:root:/root:/bin/zsh

-f /path/to/script   读取文件中的sed命令

[root@localhost ~]#cat p.sed 
1p 
2p 
$p 
[root@localhost ~]#sed -n -f p.sed  
/etc/passwd root:x:0:0:root:/root:/bin/bash 
bin:x:1:1:bin:/bin:/sbin/nologin 
apache:x:1008:1008::/home/apache:/sbin/nologin

-i 直接修改原文件,慎用

[root@localhost ~]#sed -n '/^zgx/p' /etc/passwd 
zgx:x:1007:1007::/home/zgx:/bin/bash 
[root@localhost ~]#sed -i '/^zgx/d' /etc/passwd 
[root@localhost ~]#sed -n '/^zgx/p' /etc/passwd

-r 使用扩展正则表达式

[root@localhost ~]#sed -n '/^ro\\+t/p' /etc/passwd  
root:x:0:0:root:/root:/bin/bash 
[root@localhost ~]#sed -n '/^ro+t/p' /etc/passwd  
[root@localhost ~]#sed -n -r '/^ro+t/p' /etc/passwd 
root:x:0:0:root:/root:/bin/bash

script的表示方法

address+command

address:

1、startline,endline   指定范围

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

2、/regexp/             正则匹配

[root@localhost ~]#sed -n '/^root/p' /etc/passwd 
root:x:0:0:root:/root:/bin/bash

3、/pattern1/,/pattern2/   正则匹配范围

[root@localhost ~]#sed -n '/^root/,/^lp/p' /etc/passwd     
root:x:0:0:root:/root:/bin/bash 
bin:x:1:1:bin:/bin:/sbin/nologin 
daemon:x:2:2:daemon:/sbin:/sbin/nologin 
adm:x:3:4:adm:/var/adm:/sbin/nologin 
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

4、linenumber  指定行

[root@localhost ~]#sed -n '10p' /etc/passwd 
operator:x:11:0:operator:/root:/sbin/nologin

5、startline,+n  指定起始及向后行数

[root@localhost ~]#sed -n '10,+2p' /etc/passwd 
operator:x:11:0:operator:/root:/sbin/nologin 
games:x:12:100:games:/usr/games:/sbin/nologin 
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 
[root@localhost ~]#sed -n '/^root/,+2p' /etc/passwd       
root:x:0:0:root:/root:/bin/bash 
bin:x:1:1:bin:/bin:/sbin/nologin 
daemon:x:2:2:daemon:/sbin:/sbin/nologin

6、步进行

[root@localhost ~]#sed -n '1~2p' /etc/passwd   #输出奇数行 
root:x:0:0:root:/root:/bin/bash 
daemon:x:2:2:daemon:/sbin:/sbin/nologin 
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 
games:x:12:100:games:/usr/games:/sbin/nologin 
nobody:x:99:99:Nobody:/:/sbin/nologin

command:

d 删除符合条件的行

[root@localhost ~]#sed '1d' /etc/passwd  #删除了第一行的root用户 
bin:x:1:1:bin:/bin:/sbin/nologin 
daemon:x:2:2:daemon:/sbin:/sbin/nologin

p 显示符合条件的行

[root@localhost ~]#sed '1p' /etc/issue  #模式空间与匹配到的行都输出 
\S 
\S 
Kernel \r on an \m

a \string 在指定的行后面追加新行

[root@localhost ~]#sed '1a\newline' /etc/issue  
\S 
newline 
Kernel \r on an \m

i \string  在指定的行前面插入新行

[root@localhost ~]#sed '1i\newline' /etc/issue  
newline 
\S 
Kernel \r on an \m

c \string 将匹配到的行替换为此处指定的文本text

[root@localhost ~]#sed '1c\newline' /etc/issue 
newline 
Kernel \r on an \m

r file 读取文件,合并文本

[root@localhost ~]#sed '1r /etc/issue' /etc/issue 
\S 
\S 
Kernel \r on an \m  
Kernel \r on an \m

w file 将地址指定的访问的行另存为指定文件中

[root@localhost ~]#sed '1,2 w /tmp/passwd' 
/etc/passwd root:x:0:0:root:/root:/bin/bash 
bin:x:1:1:bin:/bin:/sbin/nologin 
[root@localhost ~]#cat /tmp/passwd  
root:x:0:0:root:/root:/bin/bash 
bin:x:1:1:bin:/bin:/sbin/nologin

= 为模式匹配到的行打印出行数

[root@localhost ~]#sed '1,2=' /etc/passwd               
1 
root:x:0:0:root:/root:/bin/bash 
2 
bin:x:1:1:bin:/bin:/sbin/nologin

! 取反

[root@localhost ~]#sed -n '1!p' /etc/issue   
Kernel \r on an \m

s/pattern/string/修饰符:查找并替换,默认只替换每一行被匹配到的字符串
修饰符:
g:全局替换

[root@localhost ~]#cat root.txt  
root 
root 
txt 
txt 
test 
test 
ROOT 
[root@localhost ~]#sed  's/^root/toor/g' root.txt  
toor 
toor 
txt 
txt 
test 
test 
ROOT

i 忽略带下写

[root@localhost ~]#sed  's/^root/toor/i' root.txt   
toor 
toor 
txt 
txt 
test 
test 
toor

w /path/to/somefile 将替换成功的结果保存至指定文件中

[root@localhost ~]#sed  's/^root/toor/w /tmp/root.txt' root.txt    
toor 
toor 
txt 
txt 
test 
test 
ROOT 
[root@localhost ~]#cat /tmp/root.txt  
toor 
toor

p 显示替换成功的行

[root@localhost ~]#sed -n 's/^root/toor/p' root.txt  
toor 
toor

高级编辑命令(不常用)

  • h:表示把模式空间的内容保存至保持空间

  • H:把模式空间中的内容追加至保持空间

  • g:把保持空间的内容覆盖至模式空间

  • G:把保持空间的内容追加至模式空间

  • x:把模式空间的内容与保持空间中内容互换

  • n:读取匹配到行的下一行至模式空间

  • N: 追加读取匹配到的行的下一行至模式空间中

  • D:删除多行模式空间中所有行

实例:

sed -n 'n;p' file 显示偶数行

sed '1!G;h;$!d'  file 逆序显示文件

sed '$!d' file 取出最后一行

sed  '$!N;$!D' FILE 取出最后两行

sed '/^$/d;G' file  删除所以空白行,为每行加空白行

sed 'n;d' file:显示奇数行

原创文章,作者:N25_随心,如若转载,请注明出处:http://www.178linux.com/67014

(0)
N25_随心N25_随心
上一篇 2017-03-15 19:09
下一篇 2017-03-15

相关推荐

  • 马哥教育网络班21期+第6周课程练习

    请详细总结vim编辑器的使用并完成以下练习题 1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; [root@localhost ~]# cp /etc/rc.d/rc.sysinit /tmp/ [root@localho…

    Linux干货 2016-08-15
  • 在软raid10上使用LVM,并且进行磁盘配额,最后还原。

    实验环境 1)centos 7.3 2)lvm格式化的文件系统为ext4 3)磁盘为单一磁盘的不同分区 4)使用软件:mdadm,lvm2 tools,quota 软raid10的创建 一,两个raid1的创建 1)对磁盘进行分区,使用工具fdidk,每个大小为1GB,类型改为fd。我的分区情况:/dev/sda{6,7,8,9}将会拿来做为raid的磁盘。…

    Linux干货 2017-03-16
  • 磁盘管理的补充及扩展

    磁盘管理的补充及扩展 挂载点和/etc/fstab(配置文件) 使用mount命令挂载为临时挂载开机重启后就会自动卸载,为了永久挂载必须写在配置文件中! 配置文件系统体系  被mount、fsck和其它程序使用  系统重启时保留文件系统体系  可以在设备栏使用文件系统卷标  使用mount  -a 命令挂载/etc/fstab中的所有文件系…

    Linux干货 2016-08-30
  • sed

    复制/etc/rc.d/rc.sysinit文件到/tmp目录中,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首家#; 2.复制/boot/grub/grub.conf至/tmp中,删除/tmp/grub.conf文件中的行首的空白字符 3.删除/tmp/rc.sysinit文件中的以#开头,且后面跟了一个至少一个空白字符的行行的#…

    Linux干货 2016-12-04
  • bash中的变量

        对任何一门编程语言来说,最基础的部分就是变量。那什么事变量呢?变量就是把一个已知的可以变动的值,赋给一个固定名字的,用固定的名字代表这个可变动的值。在bash中也不例外,跟大多数编程语言一样,它也有一些自己的语法和规则 bash变量:     1.规则设置:变量和变量的值中间用=连接,=的两…

    Linux干货 2016-08-15
  • shell编程2

    组合测试条件  第一种方式:        COMMAND1 && COMMAND2 并且        COMMAND1 || COMMAND2 或者        ! COMMAND 非   &nbs…

    Linux干货 2016-08-21

评论列表(1条)

  • 马哥教育
    马哥教育 2017-04-10 15:38

    总结的很好,图文并茂,加油!!!