sed基础

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

[root@localhost fsy]# sed  -n  ‘3p’   /etc/passwd      // 只打印第三行
daemon:x:2:2:daemon:/sbin:/sbin/nologin

[root@localhost fsy]#  sed -n ‘2p’   // 只多输出了第二行
aaa
bbb
bbb
accc^H^[[3~
ssdfs

fdfd
dds

[root@localhost fsy]# sed -n ‘2p’ < /etc/passwd   //  用sed实现标准输入
bin:x:1:1:bin:/bin:/sbin/nologin

[root@localhost fsy]# sed ‘3,6=’  passwd

[root@localhost fsy]# cat /etc/passwd | sed -n ‘2p’
bin:x:1:1:bin:/bin:/sbin/nologin

[root@localhost fsy]# seq 10
1
2
3
4
5
6
7
8
9
10
[root@localhost fsy]# seq 10 | sed -n ‘5p’
5

sed可以对输出做一些处理,而不是只处理文件

[root@localhost ~]# ifconfig | sed -n 2p
          inet addr:192.168.1.128  Bcast:192.168.1.255  Mask:255.255.255.0

[root@localhost fsy]# cat -n passwd   // 空行加行号

[root@localhost fsy]# cat -b passwd   //  空行不加行号

[root@localhost fsy]# sed -n ‘/^root/p’ passwd  // 打印包含root的行
root:x:0:0:root:/root:/bin/bash

[root@localhost fsy]# sed -n ‘10,20p’ passwd   // 打印第10行到第20行的内容

[root@localhost fsy]# sed -n ’10,+20p’ passwd   //  打印第10行到第30行的内容

地址定界

[root@localhost fsy]# sed -n ‘/^h/,/^s/p’  passwd   // 打印h开头的行到s开头的行

[root@localhost fsy]# sed -n ’10,/^s/p’ passwd

[root@localhost fsy]# sed -n ‘/^s/,40p’ passwd

步进

[root@localhost fsy]# seq 10| sed -n  ‘1~2p’   // 取奇数行
1
3
5
7
9

[root@localhost fsy]# seq 10 |sed -n ‘2~2p’
2
4
6
8
10

[root@localhost ~]# seq 10 |sed ‘2~2d’
1
3
5
7
9

[root@localhost fsy]# cat passwd | sed ‘3,6a ==========================’

[root@localhost fsy]# cat passwd | sed ‘3~6a ==========================’

[root@localhost fsy]# cat passwd | sed ‘3,6a\                   ==========================’

[root@localhost fsy]# cat passwd | sed ‘3,6i\                   ==========================’            // 在前面插

[root@localhost fsy]# cat passwd | sed ‘3,6c\                   ==========================’                 // 替代

[root@localhost fsy]# cat passwd | sed ‘3,6a\   ==========================\n   +++++++++++++++++++++++’          // 插入两行

[root@localhost fsy]# cat passwd2 | sed -n ‘3,6w ff’
[root@localhost fsy]# cat ff
     3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4 adm:x:3:4:adm:/var/adm:/sbin/nologin
     5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6 sync:x:5:0:sync:/sbin:/bin/sync

[root@localhost fsy]# cat passwd2 | sed ‘3,6r /etc/issue’

[root@localhost fsy]# sed -n  ‘3,6!p’ passwd2     // 取反

[root@localhost fsy]# seq 10 |sed -n ‘2~2!p’

[root@localhost fsy]# sed ‘2d’ f1
1
3
4
5
6
7
8
9
10

[root@localhost fsy]# sed -i ‘2d’ f1          //  真的把文件给改了
[root@localhost fsy]# cat f1
1
3
4
5
6
7
8
9
10

[root@localhost fsy]# sed -i.txt ‘2d’ f1              //  先做备份再修改文件
[root@localhost fsy]# cat f1.txt
1
2
3
4
5
6
7
8
9
10

搜索替代

[root@localhost fsy]# sed ‘s/root/Root/’ passwd
[root@localhost fsy]# sed ‘s/root/Root/g’ passwd

[root@localhost fsy]# sed -n ‘s/root/Root/gp’ passwd          // 只显示替代成功的行
Root:x:0:0:Root:/Root:/bin/bash
operator:x:11:0:operator:/Root:/sbin/nologin

[root@localhost fsy]# sed -n ‘s/root/Root/w p1’ passwd     // 把结果保存到p1文件
[root@localhost fsy]# cat p1
Root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/Root:/sbin/nologin

[root@localhost fsy]# sed -n ‘s/root/Root/gw p1’ passwd
[root@localhost fsy]# cat p1
Root:x:0:0:Root:/Root:/bin/bash
operator:x:11:0:operator:/Root:/sbin/nologin

[root@localhost fsy]# sed -i.bak ‘s/6.9/9.9/g’   /etc/centos-release 
[root@localhost fsy]# cat /etc/centos-release
CentOS release 9.9 (Final)

[root@localhost fsy]# 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 these two values:

    targeted – Targeted processes are protected,

    mls – Multi Level Security protection.

SELINUXTYPE=targeted 

[root@localhost fsy]# sed ‘s/SELINUX=enforcing/SELINUX=disabled/’  /etc/selinux/config

[root@localhost fsy]# sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/’  /etc/selinux/config

46分钟有总结

[root@localhost fsy]# sed -n ‘/^$/=’ /etc/fstab   //  显示空行的行号
1
19

[root@localhost fsy]# sed -n -e ‘/^s/=’ -e ‘/^U/p’   /etc/fstab
UUID=9341e6da-5470-4df8-89c5-bfbc3a689d8d /                       ext4    defaults        1 1
UUID=12c88f0f-aa56-4be1-9c87-eaf0cf524e75 /app                    ext4    defaults        1 2
UUID=20192758-4252-4bf6-83d4-52da93ffdab4 /boot                   ext4    defaults        1 2
UUID=567c9b8b-5207-4a4c-8548-9f5b6ee49c3a /home                   ext4    defaults        1 2
UUID=3ac9cd7c-8d23-43a6-a550-9f93df25f5cf swap                    swap    defaults        0 0
16
UUID=”5263027d-f571-4c4a-98d8-930e41e0e265″  /home  ext4   usrquota,grpquota 0 0

[root@localhost ~]# sed -n ‘/^$/=’ -e ‘/^U/=’  /etc/fstab
sed: can’t read /^$/=: No such file or directory
9
10
11
12
13
18

[root@localhost ~]# sed -n -e ‘/^$/=’ -e ‘/^$/p’  /etc/fstab
1

19

4

[root@localhost ~]# ifconfig|sed -n 2p|sed  -e  ‘s@.r:@@’
192.168.1.128  Bcast:192.168.1.255  Mask:255.255.255.0

[root@localhost ~]# ifconfig|sed -n 2p|sed  -e  ‘s@.r:@@’
192.168.1.128  Bcast:192.168.1.255  Mask:255.255.255.0

[root@localhost ~]# ifconfig|sed -n 2p|sed  -e  ‘s@.r:@@’
192.168.1.128  Bcast:192.168.1.255  Mask:255.255.255.0

[root@localhost ~]# ifconfig|sed -n 2p|sed  -e  ‘s@.r:@@’ -e ‘s@B.@@’
192.168.1.128  

[root@localhost ~]# ifconfig|sed -n 2p|sed  -e  ‘s@.r:@@’ -e ‘s@B.@@’
192.168.1.128  

[root@localhost ~]# ifconfig|sed -n 2p|sed  -e  ‘s@.r:@@’ -e ‘s@B.@@’
192.168.1.128  

[root@localhost ~]# ifconfig | sed   -e  ‘/Bcast/s/.r://’  -e  ‘/Bcast/s/B.//’                 // 多点替代

[root@localhost ~]# ifconfig | sed   -e  ‘/Bcast/s/.r://’  -e  ‘/Bcast/s/B.//’

[root@localhost ~]# ifconfig eth0 | sed -e ‘s/.inet addr://’ -e  ‘/Bcast/s/B.//’  -n -e 2p
192.168.1.128  

[root@localhost ~]# ifconfig |sed -e ‘s@.addr:@@’ -e ‘s@Bcast:.@@’  -n -e 2p
192.168.1.128  

取基名
echo  /etc/sysconfig/network-scripts/ |sed -r  ‘s@(^/.
/)([^/]+/?)@\2@’

取目录名
echo  /etc/sysconfig/network-scripts/ |sed -r  ‘s@(^/.*/)([^/]+/?)@\1@’

对/etc/fstab非#开头的加注释
sed -r  ‘s/(^[^#])/#\1/’ /etc/fstab 
sed -r  ‘s/^[^#]/#&/’ /etc/fstab

下面是sed高级用法

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