码迷,mamicode.com
首页 > 其他好文 > 详细

Sed 命令

时间:2018-12-29 00:11:10      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:autoconf   中间   其它   iso   cas   base   后向引用   for   sed -n   

sed工具

? 用法:
sed [option]... ‘script‘ inputfile...

常用选项:
-n 不输出模式空间内容到屏幕,即不自动打印
-e 多点编辑
-f /PATH/SCRIPT_FILE 从指定文件中读取编辑脚本
-r 支持使用扩展正则表达式
-i.bak 备份文件并原处编辑
?script:
‘地址命令‘

地址定界:
(1) 不给地址:对全文进行处理
(2) 单地址:
#: 指定的行,$:最后一行
/pattern/:被此处模式所能够匹配到的每一行
(3) 地址范围:
#,#
#,+#
/pat1/,/pat2/
#,/pat1/
(4) ~:步进
1~2 奇数行
2~2 偶数行

编辑命令:
d 删除模式空间匹配的行,并立即启用下一轮循环
p 打印当前模式空间内容,追加到默认输出之后
a []text 在指定行后面追加文本,支持使用\n实现多行追加
i []text 在行前面插入文本
c []text 替换行为单行或多行文本
w /path/file 保存模式匹配的行至指定文件
r /path/file 读取指定文件的文本至模式空间中匹配到的行后
= 为模式空间中的行打印行号
! 模式空间中匹配行取反处理

?s/// 查找替换,支持使用其它分隔符,s@@@,s###
?替换标记:
g 行内全局替换
p 显示替换成功的行
w /PATH/FILE 将替换成功的行保存至文件中

高级编辑命令
?P: 打印模式空间开端至\n内容,并追加到默认输出之前
?h: 把模式空间中的内容覆盖至保持空间中
?H:把模式空间中的内容追加至保持空间中
?g: 从保持空间取出数据覆盖至模式空间
?G:从保持空间取出内容追加至模式空间
?x: 把模式空间中的内容与保持空间中的内容进行互换
?n: 读取匹配到的行的下一行覆盖至模式空间
?N:读取匹配到的行的下一行追加至模式空间
?d: 删除模式空间中的行
?D:如果模式空间包含换行符,则删除直到第一个换行符的模式空间中的文本,并不会读取新的输入行,而使用合成的模式空间重新启动循环。如果模式空间不包含换行符,则会像发 出d命令那样启动正常的新循环

sed使用实例:

#sed -n ‘/^[^#]/p‘ /etc/fstab // 对#号开头的行取反,
UUID=576ed6fe-d3e8-403d-832f-2dc117ba8d2f / xfs defaults 0 0
UUID=6e6694fc-5a2f-47a1-b96b-509621e5fc1e /boot xfs defaults 0 0
UUID=1828df2b-47f3-442d-9681-ca17539861d9 /data xfs defaults 0 0
UUID=111bb92e-4d79-486a-afaf-38ba79856d30 swap swap defaults 0 0

#seq 10 |sed -n ‘3,6p‘ //打印3到6行
3
4
5
6

#sed -n ‘/^b/,/^f/p‘ /etc/passwd //打印指定文件以字母b开头到字母f开头的行
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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
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

#seq 10 |sed -n ‘1~2p‘ //打印奇数行
1
3
5
7
9

#seq 10 |sed ‘2d‘ // 2d 指删除第二行
1
3
4
5
6
7
8
9
10

#seq 10 |sed ‘1~2d‘ //删除奇数行
2
4
6
8
10

#seq 10 |sed ‘2~2d‘ //删除偶数行
1
3
5
7
9

#sed ‘/^#/d‘ /etc/fstab |sed ‘/^$/d‘ //先删除以#号开头的行,再删空行
UUID=576ed6fe-d3e8-403d-832f-2dc117ba8d2f / xfs defaults 0 0
UUID=6e6694fc-5a2f-47a1-b96b-509621e5fc1e /boot xfs defaults 0 0
UUID=1828df2b-47f3-442d-9681-ca17539861d9 /data xfs defaults 0 0
UUID=111bb92e-4d79-486a-afaf-38ba79856d30 swap swap defaults 0 0

#sed ‘1d;/^#/d‘ /etc/fstab //删除指定行和以#号开头的行,中间以分;隔开
UUID=576ed6fe-d3e8-403d-832f-2dc117ba8d2f / xfs defaults 0 0
UUID=6e6694fc-5a2f-47a1-b96b-509621e5fc1e /boot xfs defaults 0 0
UUID=1828df2b-47f3-442d-9681-ca17539861d9 /data xfs defaults 0 0
UUID=111bb92e-4d79-486a-afaf-38ba79856d30 swap swap defaults 0 0

#seq 10 |sed ‘2~2ahello‘ //在偶数行后插入hello, 后面插入用a
1
2
hello
3
4
hello
5
6
hello
7
8
hello
9
10
hello

#seq 10 |sed ‘2~2a ===‘
1
2

3
4

5
6

7
8

9
10

seq 10 |sed ‘2~2a\ ===‘ //比较加转义符的区别
1
2

3
4

5
6

7
8

9
10

#sed ‘/# User/awelcome to magedu‘ ~/.bashrc //在# User行后插入welcome to magedu

.bashrc

User specific aliases and functions

welcome to magedu

alias rm=‘rm -i‘
alias cp=‘cp -i‘
alias mv=‘mv -i‘
alias xp=‘cd /etc/sysconfig/network-scripts/‘
alias tree=‘tree --charset ASCII‘

#seq 10 |sed ‘2~2ihello‘ //在偶数行前插入hello,行前插入用i
1
hello
2
3
hello
4
5
hello
6
7
hello
8
9
hello
10

#seq 10|sed ‘2~2c hello‘ //c 把偶数行替换为hello
1
hello
3
hello
5
hello
7
hello
9
hello

#seq 10 |sed ‘1~2r /etc/issue‘ //r /path/file 读取指定文件的文本至模式空间中匹配到的行后
1
CentOS release 6.10 (Final)
Kernel \r on an \m

2
3
CentOS release 6.10 (Final)
Kernel \r on an \m

4
5
CentOS release 6.10 (Final)
Kernel \r on an \m

6
7
CentOS release 6.10 (Final)
Kernel \r on an \m

8
9
CentOS release 6.10 (Final)
Kernel \r on an \m


#ifconfig eth0 |sed -n ‘2p‘
inet addr:192.168.33.135 Bcast:192.168.33.255 Mask:255.255.255.0

#ifconfig eth0 |sed -n ‘2!p‘ // ! 模式空间中匹配行取反处理
eth0 Link encap:Ethernet HWaddr 00:0C:29:71:4E:53
inet6 addr: fe80::20c:29ff:fe71:4e53/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:869 errors:0 dropped:0 overruns:0 frame:0
TX packets:550 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:81717 (79.8 KiB) TX bytes:63219 (61.7 KiB)

#sed -n ‘s/UUID/uuid/p‘ /etc/fstab // s/// 查找替换,支持使用其它分隔符,s@@@,s###
uuid=e21f9af1-9890-4abc-9a38-89be6ea69276 / ext4 defaults 1 1
uuid=aadd795d-685c-4b1b-b08f-eb0fdf3ebc8d /boot ext4 defaults 1 2
uuid=5326996d-8d2b-4c87-b819-ab3b890ed512 /data ext4 defaults 1 2
uuid=553ca22c-0b42-4256-afd3-5ef98df0fb97 swap swap defaults 0 0

备注: 替换标记
p 显示替换成功的行
w /PATH/FILE 将替换成功的行保存至文件中

#grep root /etc/passwd |sed -r ‘s/root//g‘ // 查找替换root删除 = 把root替换为空
:x:0:0::/:/bin/bash
operator:x:11:0:operator:/:/sbin/nologin

#grep root /etc/passwd |sed ‘s/root/rooter/g‘ //把root替换为rooter
rooter:x:0:0:rooter:/rooter:/bin/bash
operator:x:11:0:operator:/rooter:/sbin/nologin

#grep root /etc/passwd |sed -r ‘s/(root)/\1er/g‘ // -r 支持使用扩展正则表达式, 利用后向引用, g 行内全局替换
rooter:x:0:0:rooter:/rooter:/bin/bash
operator:x:11:0:operator:/rooter:/sbin/nologin


#sed -r "s/(^[^#])/#\1/" /etc/fstab //给不是以#号开头的行加上#号

/etc/fstab

Created by anaconda on Sun Dec 9 22:10:28 2018

#

Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#UUID=576ed6fe-d3e8-403d-832f-2dc117ba8d2f / xfs defaults 0 0 //被操作行
#UUID=6e6694fc-5a2f-47a1-b96b-509621e5fc1e /boot xfs defaults 0 0 //被操作行
#UUID=1828df2b-47f3-442d-9681-ca17539861d9 /data xfs defaults 0 0 //被操作行
#UUID=111bb92e-4d79-486a-afaf-38ba79856d30 swap swap defaults 0 0 //被操作行


#echo "/etc/rc.d/init.d/" |sed -r ‘s@(.*/)([^/]+)/?@\1@‘ //用sed取路径的目录名
/etc/rc.d/

#echo "/etc/rc.d/init.d/" |sed -r ‘s@(.*/)([^/]+)/?@\2@‘ //用sed取路径的基名
init.d

#ifconfig |sed -nr ‘2s/[^0-9]+([0-9.]+).*/\1/p‘ // 取网卡IP, CentOS 6和7 上通用
192.168.33.134


#sed -rn ‘/^#<VirtualHost/,/^<\/VirtualHost/ s/#//p‘ /etc/httpd/conf/httpd.conf //指定范围,查找替换(把注释去掉,替换为空)

#name=wang
#echo abcabc |sed "s/abc/$name/g" // 查找替换,支持变量
wangwang

#seq 10 |sed -n ‘n;p‘ //n: 读取匹配到的行的下一行覆盖至模式空间, P: 打印模式空间开端至\n内容,并追加到默认输出之前
2
4
6
8
10

#seq 10 |sed ‘1!G;h;$!d‘ //G:从保持空间取出内容追加至模式空间; h: 把模式空间中的内容覆盖至保持空间中; d: 删除模式空间中的行
10
9
8
7
6
5
4
3
2
1


#ls /misc/cd/Packages/
authconfig-gtk-6.2.8-30.el7.x86_64.rpm
authd-1.4.3-42.el7.x86_64.rpm
autoconf213-2.13-31.el7.noarch.rpm

#ls /misc/cd/Packages/ |sed -nr ‘s#(.).(.).rpm#\2#p‘ |sort -u //统计centos安装光盘中Package目录下的所有rpm文件的以.分隔倒数第二个字段.
i686
noarch
x86_64

#ls /misc/cd/Packages/ |sed -nr ‘s#(.).(.).rpm#\2#p‘ |sort |uniq -c //统计centos安装光盘中Package目录下的所有rpm文件的以.分隔倒数第二个字段的重复次数.
2223 i686
3117 noarch
4571 x86_64

===============================================================================

用正则表达式匹配手机号码

#grep -oE "1[0-9]{10}" tel.txt

用正则表达式匹配email邮箱

#egrep -o "([[:alnum:]]|_)+@[[:alnum:]]+.com$" mail.txt

使用正则正则取IP

#ifconfig |grep -Eo "([0-9]{1,3}.){3}[0-9]{1,3}"

=================================================================================

yum源配置

[root@centos6 yum.repos.d]#cat epel.repo //epel源
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/6/x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-6Server
enabled=1

[root@centos6 yum.repos.d]#cat aliyun.repo //aliyun源
[aliyun]
name=aliyun
baseurl=https://mirrors.aliyun.com/centos/7.5.1804/os/x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/7.5.1804/os/x86_64/RPM-GPG-KEY-CentOS-7
enabled=1

[root@centos6 yum.repos.d]#cat nginx.repo //nginx源
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1

[root@centos6 yum.repos.d]#cat cdrom.repo //本地源
[cdrom]
name=cdrom repo
baseurl=file:///misc/cd
gpgkey=file:///misc/cd/RPM-GPG-KEY-CentOS-6
gpgcheck=1
enabled=1

============================================================================================

Centos7.5 实现光盘自动挂载

[root@centos7 ~]#rpm -q autofs //查看autofs包是否安装
package autofs is not installed

[root@centos7 ~]#yum -y install autofs //yum安装autofs包

[root@centos7 ~]#systemctl enable autofs; systemctl start autofs

[root@centos7 ~]#cd /misc/cd //只要访问/misc/cd这个目录,光盘就会自动挂载

[root@centos7 cd]#ls
CentOS_BuildTag EULA images LiveOS repodata RPM-GPG-KEY-CentOS-Testing-7
EFI GPL isolinux Packages RPM-GPG-KEY-CentOS-7 TRANS.TBL

[root@centos7 cd]#df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 52403200 5935708 46467492 12% /
devtmpfs 483096 0 483096 0% /dev
tmpfs 498988 0 498988 0% /dev/shm
tmpfs 498988 14948 484040 3% /run
tmpfs 498988 0 498988 0% /sys/fs/cgroup
/dev/sda3 31441920 33388 31408532 1% /data
/dev/sda1 1038336 159296 879040 16% /boot
tmpfs 99800 40 99760 1% /run/user/0
/dev/sr0 9176232 9176232 0 100% /misc/cd //验证已经挂载

Sed 命令

标签:autoconf   中间   其它   iso   cas   base   后向引用   for   sed -n   

原文地址:http://blog.51cto.com/8845692/2336603

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!