Linux Basic - sed Stream EDitor流编辑器,sed简介、sed基本用法、sed高级用法、sed地址定界、sed示例、

简单介绍

行编辑器;逐行编辑的,一次只处理一行文本,不做原文件编辑,是放置在sed的内存空间进行编辑的,在内存中进行编辑,编辑完成后,显示到屏幕中,这是默认的工作方式。

特性介绍

- 默认不直接编辑原文件
- 默认直接输出至屏幕
- 默认可以使用基本正则表达式

man sed 之 常用选项介绍

1. 用法:

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

2. 常用 OPTION

选项长选项意义
-n–quiet, --silent不输出模式中的内容至屏幕
-escript, --expression=script指定运行脚本,属于多点编辑功能
-fscript-file, --file=script-file/PATH/TO/SCRIPT_FILE:从指定文件中读取脚本
-i [SUFFIX]–in-place[=SUFFIX]直接编辑原文件
-l N–line-length=N直接指定行的长度
-E/-r–regexp-extende使用扩展正则表达式—也就是说默认使用的是基本的正则表达式
–sandbox 、沙箱模式,禁用/e/r/w 命令

3.编辑命令

零地址命令

+	\:label		
	+用于“b” 和 “t” 两个命令的标记
+	#comment
	+ 注释延伸至下一行(或者到-e 选项的尾部)
+	}
	+ 用于{ }关闭

零地址 或 一地址 命令–sed ‘/^UUID/a #Hello,World’ /etc/fstab

+ =	: 打印当前的行号(在模式空间中的行号)
+ a	: append的意思,将内容追加至全部匹配到的行,例如sed '/^UUID/a \#Hello World' /etc/fstab , 可以追加多行,使用\n进行换行
+ i	:insert,表示 在符合条件的前面插入内容,支持\n 多行插入
+ r	:从另外一个文件读取的内容添加至匹配到的位置后
+ R :读取另外一个文件的每一行添加至匹配到的每一行后

#### a 命令添加一行
[root@Private ~]# sed '/^UUID/a \#Hello,World' /etc/fstab		# 在所有以UUID形状的行后都加一个#Hello,World,未匹配到的不加

#
# /etc/fstab
# Created by anaconda on Mon Mar 23 19:58:49 2020
#
# 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=68739322-a4f0-4948-9ca2-5109d92f1101 /                       xfs     defaults        0 0
#Hello,World
UUID=66e47263-818b-4ea9-9eaa-205959c00600 /boot                   xfs     defaults        0 0
#Hello,World
UUID=ea2f443d-cc9b-4884-ad9e-cb7fd5267bab /data                   xfs     defaults        0 0
#Hello,World
UUID=0aaffad2-293d-4e77-b8cb-ac17b8d83b35 swap                    swap    defaults        0 0
#Hello,World
/dev/sdc1                               /data2                  xfs     defaults        0 0
[root@Private ~]# 
a 命令添加多行–sed ‘/^UUID/a #Hello,World\n#This is sed test.’ /etc/fstab
[root@Private ~]# sed '/^UUID/a \#Hello,World\n\#This is sed test.' /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Mon Mar 23 19:58:49 2020
#
# 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=68739322-a4f0-4948-9ca2-5109d92f1101 /                       xfs     defaults        0 0
#Hello,World
#This is sed test.
UUID=66e47263-818b-4ea9-9eaa-205959c00600 /boot                   xfs     defaults        0 0
#Hello,World
#This is sed test.
UUID=ea2f443d-cc9b-4884-ad9e-cb7fd5267bab /data                   xfs     defaults        0 0
#Hello,World
#This is sed test.
UUID=0aaffad2-293d-4e77-b8cb-ac17b8d83b35 swap                    swap    defaults        0 0
#Hello,World
#This is sed test.
/dev/sdc1                               /data2                  xfs     defaults        0 0
[root@Private ~]# 
i 命令添加一行–sed "/^UUID/i #before uuid " /etc/fstab
[root@Private ~]# sed "/^UUID/i \#before uuid " /etc/fstab

#
# /etc/fstab
# Created by anaconda on Mon Mar 23 19:58:49 2020
#
# 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
#
#before uuid 
UUID=68739322-a4f0-4948-9ca2-5109d92f1101 /                       xfs     defaults        0 0
#before uuid 
UUID=66e47263-818b-4ea9-9eaa-205959c00600 /boot                   xfs     defaults        0 0
#before uuid 
UUID=ea2f443d-cc9b-4884-ad9e-cb7fd5267bab /data                   xfs     defaults        0 0
#before uuid 
UUID=0aaffad2-293d-4e77-b8cb-ac17b8d83b35 swap                    swap    defaults        0 0
/dev/sdc1                               /data2                  xfs     defaults        0 0
[root@Private ~]# 
i 命令添加多行–sed “/^UUID/i #before uuid\n#test secondary line.” /etc/fstab
[root@Private ~]# sed "/^UUID/i \#before uuid\n\#test secondary line." /etc/fstab       

#
# /etc/fstab
# Created by anaconda on Mon Mar 23 19:58:49 2020
#
# 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
#
#before uuid
#test secondary line.
UUID=68739322-a4f0-4948-9ca2-5109d92f1101 /                       xfs     defaults        0 0
#before uuid
#test secondary line.
UUID=66e47263-818b-4ea9-9eaa-205959c00600 /boot                   xfs     defaults        0 0
#before uuid
#test secondary line.
UUID=ea2f443d-cc9b-4884-ad9e-cb7fd5267bab /data                   xfs     defaults        0 0
#before uuid
#test secondary line.
UUID=0aaffad2-293d-4e77-b8cb-ac17b8d83b35 swap                    swap    defaults        0 0
/dev/sdc1                               /data2                  xfs     defaults        0 0
r 命令读取另外一个文件–sed “/2020$/r /etc/rc.local” /etc/fstab
将/etc/rc.local文件中的内容添加到/etc/fstab文件中以2020结尾的行后
[root@Private ~]# sed "/2020$/r /etc/rc.local" /etc/fstab  #将/etc/rc.local文件中的内容添加到/etc/fstab文件中以2020结尾的行后

#
# /etc/fstab
# Created by anaconda on Mon Mar 23 19:58:49 2020
===========================================编辑时手动添加的分割符,易于查看====================================
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
===========================================编辑时手动添加的分割符,易于查看====================================
#mount.cifs //172.16.133.55/feihuang /data3/ -o username=apache,password=
#
# 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=68739322-a4f0-4948-9ca2-5109d92f1101 /                       xfs     defaults        0 0
UUID=66e47263-818b-4ea9-9eaa-205959c00600 /boot                   xfs     defaults        0 0
UUID=ea2f443d-cc9b-4884-ad9e-cb7fd5267bab /data                   xfs     defaults        0 0
UUID=0aaffad2-293d-4e77-b8cb-ac17b8d83b35 swap                    swap    defaults        0 0
/dev/sdc1                               /data2                  xfs     defaults        0 0
[root@Private ~]# 
[root@Private ~]# 
R 命令读取另外一插入到匹配到的每一行-sed “/^UUID/R /etc/rc.local” /etc/fstab
[root@Private ~]# sed "/^UUID/R /etc/rc.local" /etc/fstab     

#
# /etc/fstab
# Created by anaconda on Mon Mar 23 19:58:49 2020
#
# 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=68739322-a4f0-4948-9ca2-5109d92f1101 /                       xfs     defaults        0 0
#!/bin/bash
UUID=66e47263-818b-4ea9-9eaa-205959c00600 /boot                   xfs     defaults        0 0
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
UUID=ea2f443d-cc9b-4884-ad9e-cb7fd5267bab /data                   xfs     defaults        0 0
#
UUID=0aaffad2-293d-4e77-b8cb-ac17b8d83b35 swap                    swap    defaults        0 0
# It is highly advisable to create own systemd services or udev rules
/dev/sdc1                               /data2                  xfs     defaults        0 0

地址范围命令

  • { :一段开始的命令块,使用“}" 结束
  • b :标签,分支的标签
  • c : change,替换符合条件的行为单行或多行文本
  • h : 将模式空间中的内容添加至------------->hold
  • d : 删除符合条件的行
  • g : 拷贝/附加“保留空间”至“模式空间”(覆盖式)------->get
  • G : 拷贝/附加“保留空间”至“模式空间”(追加式)------->get
  • n : 读取 匹配到的行的下一行至模式空间
  • N : 附加 匹配到的行的下一行至模式空间,其实就是读两行
  • p : 打印当前模式空间
  • w filename :把模式空间的中的内内容容写到其它文件中去。
  • x : 模式空间与保持空间的内容进行互换
  • y/source/dest/ : 替换指定的字符
  • !:地址空间取反

注意:默认就是打印,所以可以会出现同样的内容两遍,所以一般与-n(静默模式)一起使用

  • s/regexp/replacement/ : 查找指定内容,并替换为指定的内容
  				i) 查找替换,也支持使用其它符号做为分割符
  				ii) g,行内全局替换
  				iii) i:忽略字符大小写
  				iv) &引用前面查找到的所有内容    sed 's/r..t/&er/' /etc/passwd      ----->没有加g选项,只替换每一行的第一次
  				v) p:显示替换成功的行,通常与-n一起使用
  				vi) w /path/to/somewhere  : 将替换成功的结果保存至指定文件中;
			(1) ! :地址空间取反
C 修改匹配到的行为指定内容—sed “/^UUID/c #test line” /etc/fstab
[root@Private ~]# sed "/^UUID/c #test line" /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Mon Mar 23 19:58:49 2020
#
# 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
#
#test line
#test line
#test line
#test line
/dev/sdc1                               /data2                  xfs     defaults        0 0
C 修改指定行为指定内容–sed 9,11c\ “#test,line” /etc/fstab

修改第9行到第11号为test.line

[root@Private ~]# sed 9,11c\ "#test,line" /etc/fstab  

#
# /etc/fstab
# Created by anaconda on Mon Mar 23 19:58:49 2020
#
# 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
#
#test,line
UUID=0aaffad2-293d-4e77-b8cb-ac17b8d83b35 swap                    swap    defaults        0 0
/dev/sdc1                               /data2                  xfs     defaults        0 0
d 删除匹配到的行–sed “/^UUID/d” /etc/fstab
[root@Private ~]# sed "/^UUID/d" /etc/fstab  

#
# /etc/fstab
# Created by anaconda on Mon Mar 23 19:58:49 2020
#
# 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
#
/dev/sdc1                               /data2                  xfs     defaults        0 0
d 删除指定行号的内容-sed “9,11d” /etc/fstab
[root@Private ~]# sed "9,11d" /etc/fstab        

#
# /etc/fstab
# Created by anaconda on Mon Mar 23 19:58:49 2020
#
# 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=0aaffad2-293d-4e77-b8cb-ac17b8d83b35 swap                    swap    defaults        0 0
/dev/sdc1                               /data2                  xfs     defaults        0 0
p 命令打印指定的行,不加-n ------------sed “/^UUID/p” /etc/fstab
[root@Private ~]# sed  "/^UUID/p" /etc/fstab   

#
# /etc/fstab
# Created by anaconda on Mon Mar 23 19:58:49 2020
#
# 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=68739322-a4f0-4948-9ca2-5109d92f1101 /                       xfs     defaults        0 0
UUID=68739322-a4f0-4948-9ca2-5109d92f1101 /                       xfs     defaults        0 0
UUID=66e47263-818b-4ea9-9eaa-205959c00600 /boot                   xfs     defaults        0 0
UUID=66e47263-818b-4ea9-9eaa-205959c00600 /boot                   xfs     defaults        0 0
UUID=ea2f443d-cc9b-4884-ad9e-cb7fd5267bab /data                   xfs     defaults        0 0
UUID=ea2f443d-cc9b-4884-ad9e-cb7fd5267bab /data                   xfs     defaults        0 0
UUID=0aaffad2-293d-4e77-b8cb-ac17b8d83b35 swap                    swap    defaults        0 0
UUID=0aaffad2-293d-4e77-b8cb-ac17b8d83b35 swap                    swap    defaults        0 0
/dev/sdc1                               /data2                  xfs     defaults        0 0\
p 命令打印指定的行,加-n ------------sed “/^UUID/p” /etc/fstab
[root@Private ~]# sed -n "/^UUID/p" /etc/fstab 
UUID=68739322-a4f0-4948-9ca2-5109d92f1101 /                       xfs     defaults        0 0
UUID=66e47263-818b-4ea9-9eaa-205959c00600 /boot                   xfs     defaults        0 0
UUID=ea2f443d-cc9b-4884-ad9e-cb7fd5267bab /data                   xfs     defaults        0 0
UUID=0aaffad2-293d-4e77-b8cb-ac17b8d83b35 swap                    swap    defaults        0 0
y 替换指定的字符------------sed “y/xfs/ext/” /etc/fstab
[root@Private ~]# sed "y/xfs/ext/" /etc/fstab      

#
# /etc/xttab
# Created by anaconda on Mon Mar 23 19:58:49 2020
#
# Accettible xiletyttemt, by rexerence, are maintained under '/dev/ditk'
# See man paget xttab(5), xindxt(8), mount(8) and/or blkid(8) xor more inxo
#
UUID=68739322-a4x0-4948-9ca2-5109d92x1101 /                       ext     dexaultt        0 0
UUID=66e47263-818b-4ea9-9eaa-205959c00600 /boot                   ext     dexaultt        0 0
UUID=ea2x443d-cc9b-4884-ad9e-cb7xd5267bab /data                   ext     dexaultt        0 0
UUID=0aaxxad2-293d-4e77-b8cb-ac17b8d83b35 twap                    twap    dexaultt        0 0
/dev/tdc1                               /data2                  ext     dexaultt        0 0
[root@Private ~]# 

4. 地址定界

	a. 不给地址:对全文进行处理
	b. 单地址:
		i. #:指定的行
		ii. /pattern/: 被此处模式所能免匹配到的每一行;
		III : $ : 最后一行
	c. 地址范围:
		i. #,#	:从某一行到另一行
		ii. #,+#:从#行,另外加上多少行
		iii. /pat1/,/pat2/:第模式1匹配到的行到模式二匹配到的行
		iv. #,/pat1/:从#行到指定模式匹配到的行
	d. ~#:步进
		i. 隔#读取一下
			(1) 1,~2 :从第一行开始,步进为2 ,就是隔一行取一行,即取奇数行
			(2) 2,~2:从第二行开始,步进为2 ,就是隔一行取一行,即取偶数数行

部分示例:

	a. 'sed /^UUID/d' /etc/fstab   :删除文件中以UUID开头的行
	b. 'sed /^#/d' /etc/fstab   :删除文件中以#开头的行
	c. 'sed /^$/d' /etc/fstab   :删除文件中空白行
	d. 删除/boot/grub/grub.conf文件中所有以空白开头的行行首的空白字符
		 sed 's/^[[:space:]]\+//' /etc/grub2.cfg
	e. 删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符;
		 sed 's/^#[[:space:]]\+//' /etc/fstab 
	f. echo 一个绝对对路径给sed命令,取出其碁名;
			echo "/etc/fstab" | sed 's@[^/]\+$@@'
			/etc/
			
			取目录名就是把右侧给删掉就可以了
			
			[root@whzabbix ~]# echo "/etc/fstab/" | sed 's@[^/]\+/\?$@@'
			/etc/
				所有以非路径分割符组成的,并且出现在末尾的即可
			[root@whzabbix ~]# echo "/etc/fstab" | sed 's@[^/]\+/\?$@@'      最后一个斜线可有可无
			
			/etc/
			[root@whzabbix ~]# 
			
[root@whzabbix ~]# sed -n 's@r..t@&er@p' /etc/passwd    //-n 静默模式,就是默认不输出到屏幕的,后面跟个命令p,将匹配到的行再次进行显示
rooter:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/rooter:/sbin/nologin
ftp:x:14:50:FTP User:/var/fterp:/sbin/nologin

部分高级用法

7. sed -n  'n;p' FILE     读取第一行,显示行二行,读取第三行,显示第四行,显示偶数行。

8. sed '1!G;h;$!d' FILE
	a. 1!   : 第一行除外
	b. G:把保持空间中的内容追加至模式空间中来
	c. h:把模式空间中的内容覆盖保存至保持空间
	d. $:表示最后一行
	e. $!:除了最后一行
	f. d:把模式空间中的删了。
	g. 把FILE中的内容倒过来,其实跟tac一样了,tac就可以反过来显示。
9. sed ' $!N;$!D' FILE----->取文件后两行
	a. 只要不是最后一行,就读进来追加,然后删掉
	b. 读到倒数第二行不是最后一行,然后追加读一行,是最后 行,然后没有动作,就是显示。
10. sed '$!d'------------取文件最后一行
11. sed 'G' FILE------->第一行后追加一行空白行
12. sed 'g' FILE------->全部替换成空白行
13. sed '/^$/d;G' FILE----->把多个空白行合并成一个,每一行后面追加一个空白行
14. sed 'n;d' FILE;--------->只显示基数行
15. sed -n '1!G;h;$p' FILE  ——>逆序显示
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值