1
、把
/etc/passwd
复制到
/root/test.txt
,用
sed
打印所有行;
[root@172 ~]
# sed '1,$ w /root/test.txt' /etc/passwd
[root@172 ~]
# sed -n -e 'p' -e'w ./t1.txt' /etc/passwd
2
、打印
test.txt
的
3
到
10
行
;
[root@172 ~]
# sed -n '3,10p' test.txt
3
、打印
test.txt
中包含
’root’
的行;
[root@172 ~]
# sed -n '/root/p' test.txt
4
、删除
test.txt
的
15
行以及以后所有行;
[root@172 ~]
# sed '15,$d' test.txt
5
、删除
test.txt
中包含
’bash’
的行;
[root@172 ~]
# sed '/bash/d' test.txt
6
、替换
test.txt
中
’root’
为
’toor’
;
[root@172 ~]
# sed -n 's/root/toor/g p' test.txt
7
、替换
test.txt
中
’/sbin/nologin’
为
’/bin/login’
sed
's/\/sbin\/nologin/\/bin\/login/g'
test.txt
8
、删除
test.txt
中
5
到
10
行中所有的数字;
[root@172 ~]
# sed -nr '5,10 s/[0-9]+//g p' test.txt
[root@172 ~]
# sed -nr '5,10 s/[0-9]*//g p' test.txt
9
、删除
test.txt
中所有特殊字符(除了数字以及大小写字母);
[root@172 ~]
# sed -nr 's/[[:punct:]]//g p' test.txt
10
、在
test.txt
20
行到末行最前面加
’aaa:’
[root@172 ~]
# sed '20,$ i aaa:' test.txt
11
、删除
centos7
系统
/etc/grub2.cfg
文件中所有以空白开头的行行首的空白字符
[root@172 ~]
# sed 's/^[ ]*//' /etc/grub2.cfg
12
、删除
/etc/fstab
文件中所有以
'#'
开头,后面至少跟一个空白字符的行的行首的
'#'
和空白
字符
[root@172 ~]
# sed -r 's/^#[ ]//' /etc/fstab
13
、在
centos6
系统
/root/install.log
每一行行首增加
'#'
号
[root@centos6 ~]
# sed -n 's/^/#/p' install.log
[root@centos6 ~]
# sed -rn 's/(^.*)/#\1/p' install.log
14
、在
/etc/fstab
文件中不以
'#'
开头的行的行首增加
'#'
号
[root@172 ~]
# sed 's/^[^#]/#/' /etc/grub2.cfg
15
、处理
/etc/fstab
路径
,
使用
grep
和
sed
命令取出其目录名和基名
[root@centos7 data]
#echo /etc/fstab | sed -nr 's#(.*)/.*$#\1#p' /etc
[root@centos7 data]
#echo /etc/fstab | sed -nr 's#.*/([^/]+)/?$#\1#p'
fstab
16
、利用
sed
取出
ifconfig
命令中本机的
IPv4
地址
[root@172 ~]
# ifconfig |sed -nr '/inet / s/.*et (.*) ne.*/\1/ p'
17
、统计
centos
安装光盘中
Package
目录下的所有
rpm
文件的以
.
分隔倒数第二个字段的重复次
数
[root@172 ~]
# ls /mnt/BaseOS/Packages/ |sed -nr 's/.*el8\.(.*)\.rpm/\1/
p'