长沙企业网站建设品牌,如何确认建设银行网站不是假的,培训机构需要什么资质,大庆做网站的一 sed介绍
sed全称#xff08;stream editor#xff09;流式编辑器#xff0c;Sed主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等#xff0c;工作流程如下
sed 是一种在线的、非交互式的编辑器#xff0c;它一次处理一行内容。处理时#…一 sed介绍
sed全称stream editor流式编辑器Sed主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等工作流程如下
sed 是一种在线的、非交互式的编辑器它一次处理一行内容。处理时把当前处理的行存储在
临时缓冲区中称为“模式空间”pattern space接着用sed命令处理缓冲区中的内容处理完
成后把缓冲区的内容送往屏幕。接着处理下一行这样不断重复直到文件末尾。文件内容并没有
改变除非你使用重定向存储输出,或者使用sed -i选项
-i选项就是将本该输出到屏幕上的内容输出/流入文件中 sed命令格式如下
sed [options] command file(s)
sed [options] -f scriptfile file(s)# 注
sed和grep不一样不管是否找到指定的模式它的退出状态都是0
只有当命令存在语法错误时sed的退出状态才不是0二 sed选项与基本用法示例
2.1 sed选项
数字
选项 功能
-e 允许多项编辑
-n 取消默认的输出(模式空间的内容输出)
-i inplace就地编辑
-r 支持扩展元字符
-f 指定sed脚本文件名示例
[rootlocalhost ~]# cat a.txt
111
222
333
[rootlocalhost ~]# sed 1p a.txt
111
111
222
333
[rootlocalhost ~]# sed -r 1p a.txt
111
111
222
333
[rootlocalhost ~]# sed -rn 1p a.txt
111
[rootlocalhost ~]# sed -rni 1p a.txt
[rootlocalhost ~]# cat a.txt
111
示例— 可以用隔开执行多个命令
[rootlocalhost ~]# cat a.txt
111
222
333
444
555
666
777
[rootlocalhost ~]# sed a.txt
111
222
333
444
555
666
777
[rootlocalhost ~]# sed 1p a.txt
111
111
222
333
444
555
666
777
[rootlocalhost ~]# sed 2d a.txt
111
333
444
555
666
777
#####多重编辑命令
[rootlocalhost ~]# sed -e 3d -e 3p a.txt
111
222
444
555
666
777
[rootlocalhost ~]# sed -e 3p -e 3d a.txt
111
222
333
444
555
666
777
[rootlocalhost ~]# sed -e 4p;4d a.txt
111
222
333
444
555
666
777
示例----可以写一个sed规则
[rootlocalhost ~]# vim a.sed
[rootlocalhost ~]# sed -r -f a.sed a.txt
111
222
444
555
666
777####多重编辑命令
[rootlocalhost ~]# sed -e 3d -e 3p a.txt
111
222
444
555
666
777
示例
[rootlocalhost ~]# cat a.txt
111
222
333
444
555
666
777
[rootlocalhost ~]# sed a.txt
111
222
333
444
555
666
777
###打印第一行
[rootlocalhost ~]# sed 1d a.txt
222
333
444
555
666
777
###打印第一行以及第三行
[rootlocalhost ~]# sed 1d;3d a.txt
222
444
555
666
777
####打印1-5行
[rootlocalhost ~]# sed 1,5d a.txt
666
777
###打印3到最后一行
[rootlocalhost ~]# sed 3,$d a.txt
111
222
正则表达式
与grep一样sed在文件中查找模式时也可以使用正则表达式(RE)和各种元字符。正则表达式是
括在斜杠间的模式用于查找和替换以下是sed支持的元字符。使用基本元字符集
^, $, ., *, [], [^], \ \,\(\),\{\}使用扩展元字符集
?, , { }, |, ( )使用扩展元字符的方式
转义如\
-r参数如sed -r
示例 -- 用法 \转义
sed ‘#/etc/passwd#d’ 2.txt
sed ‘//etc/passwd/d’ 2.txt
[rootlocalhost ~]# cat 2.txt
/etc/passwd
/etc
/passwd
abcd
111
222[rootlocalhost ~]# sed \#/etc/passwd#d 2.txt
/etc
/passwd
abcd
111
222
[rootlocalhost ~]# sed /\/etc\/passwd/d 2.txt
/etc
/passwd
abcd
111
222
数字加正则
[rootlocalhost ~]# vim 1.txt
[rootlocalhost ~]# cat 1.txt
egon
111egon222
egon333egon
egon4444
6666666egon
egon5555egn
[rootlocalhost ~]# sed -r 3,/n$/d 1.txt ####离最近的n结尾删除
egon
111egon222
egon5555egn
三 sed常用命令
sed命令告诉sed对指定行进行何种操作包括打印、删除、修改等。
命令 功能
a 在当前行后添加一行或多行
c 用新文本修改替换当前行中的文本
d 删除行
i 在当前行之前插入文本
l 会用$符号标识出文件中看不到的字符的位置
p 打印行
n 把下一行内容读入模式空间后续的处理命令处理的都是刚读入的新内容
q 结束或退出sed不会将后续内容读入模式空间
r 从文件中读
! 对所选行以外的所有行应用命令
s 用一个字符串替换另一个
w 将行写入文件
y 将字符转换为另一字符不支持正则表达式,y/egon/1234/ e-1 g-2 o-3 n-4h 把模式空间里的内容复制到暂存缓冲区(覆盖)
H 把模式空间里的内容追加到暂存缓冲区
g 取出暂存缓冲区的内容将其复制到模式空间覆盖该处原有内容
G 取出暂存缓冲区的内容将其复制到模式空间追加在原有内容后面
x 交换暂存缓冲区与模式空间的内容替换标志 s
g 在行内进行全局替换
i 忽略大小写sed命令示例
打印命令p
# sed -r /egon/p a.txt
# sed -r -n /egon/p a.txt删除命令d注意用单引号
# sed -r 3d a.txt
# sed -r 3,$d a.txt
# sed -r $d a.txt
# sed -r /egon/d a.txt
# sed -r 1,/egon/{/egon/d} a.txt # 只删除模式匹配成功的第一行
删除打印
[rootlocalhost ~]# cat 1.txt
egon
111egon222
egon333egon
egon4444
6666666egon
egon5555egn
执行多个命令---- sed -r ‘1,3{p;d}’ 1.txt
[rootlocalhost ~]# sed -r 1,3{p;d} 1.txt
egon
111egon222
egon333egon
egon4444
6666666egon
egon5555egn[rootlocalhost ~]# sed -r 1,3{p;d} 1.txt
egon
111egon222
egon333egon
egon4444
6666666egon
egon5555egn替换命令s
[rootlocalhost ~]# cat 1.txt
egon
111egon222
egon333egon
egon4444
6666666egon
egon5555egn[rootlocalhost ~]# sed -r s/egon/EGON/g 1.txt
EGON
111EGON222
EGON333EGON
EGON4444
6666666EGON
EGON5555egn[rootlocalhost ~]# sed -r 1,3s/egon/EGON/g 1.txt
EGON
111EGON222
EGON333EGON
egon4444
6666666egon
egon5555egn[rootlocalhost ~]# sed -r /^egon/s/egon/EGON/g 1.txt
EGON
111egon222
EGON333EGON
EGON4444
6666666egon
EGON5555egn[rootlocalhost ~]# sed /^egon/s/^egon/EGON/g 1.txt
EGON
111egon222
EGON333egon
EGON4444
6666666egon
EGON5555egn####不去分大小写[rootlocalhost ~]# sed -r s/egon/EGON/gi 1.txt
EGON
111EGON222
EGON333EGON
EGON4444
6666666EGON
EGON5555egn
88888EGON
999000EGON
符号配合正则的用法
把要替换的内容放在符号里加后缀sed -r ‘s/[0-9]$/.change/gi’ 1.txt
[rootlocalhost ~]# sed -r 1.txt
egon
111egon2
egon333egon
egon4
6666666egon
egon5555egn
88888EGon
999000Egon
11111[rootlocalhost ~]# sed -r s/[0-9]$/.change/gi 1.txt
egon
111egon2.change
egon333egon
egon4.change
6666666egon
egon5555egn
88888EGon
999000Egon
11111.change
[rootlocalhost ~]# sed -r s/[0-9]$/-.change/gi 1.txt
egon
111egon2-.change
egon333egon
egon4-.change
6666666egon
egon5555egn
88888EGon
999000Egon
11111-.change
把字母换成数字把数字换成字母------ sed -r ‘s/([a-zA-Z])([^a-zA-Z])/\2\1/g’ 1.tx
[rootlocalhost ~]# cat 1.txt
egon
111egon2
egon333egon
egon4
6666666egon
egon5555egn
88888EGon
999000Egon
11111
[rootlocalhost ~]# sed -r s/([a-zA-Z])([^a-zA-Z])/\2\1/ 1.txt
egon
1112egon
333egonegon
4egon
6666666egon
5555egonegn
88888EGon
999000Egon
11111[rootlocalhost ~]# sed -r 1.txt
egon
111egon2
egon333egon4444
egon4
6666666egon
egon5555egn
88888EGon
999000Egon
11111
[rootlocalhost ~]# sed -r s/([a-zA-Z])([^a-zA-Z])/\2\1/g 1.txt
egon
1112egon
333egon4444egon
4egon
6666666egon
5555egonegn
88888EGon
999000Egon
11111
多重编辑命令e sed -r -e 1,3d -e s/[Ee]gon/EGON/g a.txt # 在前一个-e的基础之上进行第二个-e操作sed -r 1,3d;s/[Ee]gon/EGON/g a.txtsed -r 3{s/[0-9]/x/g;s/[Ee]gon/EGON/g} a.txt # 只处理第三行sed -r 1,3{s/[0-9]/x/g;s/[Ee]gon/EGON/g} a.txt # 处理1到3行#sed -r -n 1p;p a.txt # ;分隔依次运行先针对第一行进行p操作再针对所有行进行p操作sed -r -n 1{p;p} a.txt # 只针对第一行连续进行两次p操作[rootlocalhost ~]# sed a.txt
111
222
333
444
555
666
777[rootlocalhost ~]# sed -e 3d -e 3p a.txt
111
222
444
555
666
777
[rootlocalhost ~]# sed -e 3p -e 3d a.txt
111
222
333
444
555
666
777
[rootlocalhost ~]# sed -e 4p;4d a.txt
111
222
333
444
555
666
777
执行多条指令先打印再替换----sed -r 1,5’{p;s/egon/xxx/g}’ 1.txt
[rootlocalhost ~]# sed -r 1.txt
egon
111egon2
egon333egon4444
egon4
6666666egon
egon5555egn
88888EGon
999000Egon
11111
[rootlocalhost ~]# sed -r 1,5{p;s/egon/xxx/g} 1.txt
egon
xxx
111egon2
111xxx2
egon333egon4444
xxx333xxx4444
egon4
xxx4
6666666egon
6666666xxx
egon5555egn
88888EGon
999000Egon
11111
取反
[rootlocalhost ~]# sed -r 1.txt
egon
111egon2
egon333egon4444
egon4
6666666egon
egon5555egn
88888EGon
999000Egon
11111
[rootlocalhost ~]# sed -r 1,8d 1.txt
11111[rootlocalhost ~]# sed -r 1,8!d 1.txt
egon
111egon2
egon333egon4444
egon4
6666666egon
egon5555egn
88888EGon
999000Egon
追加内容 a
a代表在下面一行加入内容
i 代表在上一行插入内容
c 代表更换
[rootlocalhost ~]# sed -r 1.txt
egon
111egon2
egon333egon4444
egon4
6666666egon
egon5555egn
88888EGon
999000Egon
11111
[rootlocalhost ~]# sed -r 2a xxxxxxxxxx 1.txt
egon
111egon2
xxxxxxxxxx
egon333egon4444
egon4
6666666egon
egon5555egn
88888EGon
999000Egon
11111[rootlocalhost ~]# sed -r 2i xxxxxxxxx 1.txt
egon
xxxxxxxxx
111egon2
egon333egon4444
egon4
6666666egon
egon5555egn
88888EGon
999000Egon
11111
[rootlocalhost ~]# sed -r 2c xxxxxxxxx 1.txt
egon
xxxxxxxxx
egon333egon4444
egon4
6666666egon
egon5555egn
88888EGon
999000Egon
11111
插入命令i
[rootlocalhost ~]# sed -ri 2a xxxxxxxxx 1.txt
[rootlocalhost ~]# cat 1.txt
egon
111egon2
xxxxxxxxx
egon333egon4444
egon4
6666666egon
egon5555egn
88888EGon
999000Egon
11111
读文件命令
[rootlocalhost ~]# cat 1.conf http{LISTEN 8080server {....}}
[rootlocalhost ~]# cat 1.txt
egon
111egon2
egon333egon4444
egon4
6666666egon
egon5555egn
88888EGon
999000Egon
11111
[rootlocalhost ~]# sed -r 9r 1.conf 1.txt
egon
111egon2
egon333egon4444
egon4
6666666egon
egon5555egn
88888EGon
999000Egon
11111http{LISTEN 8080server {....}}
写文件命令
[rootlocalhost ~]# sed 1,3w /tmp/a.conf 1.txt
egon
111egon2
egon333egon4444
egon4
6666666egon
egon5555egn
88888EGon
999000Egon
11111
[rootlocalhost ~]# cat /tmp/a.conf
egon
111egon2
egon333egon4444
退出命令—了解即可
[rootlocalhost ~]# cat 1.txt
egon
111egon2
egon333egon4444
egon4
6666666egon
egon5555egn
88888EGon
999000Egon
11111
[rootlocalhost ~]# sed -r 1,3s/egon/XXXX/g 1.txt
XXXX
111XXXX2
XXXX333XXXX4444
egon4
6666666egon
egon5555egn
88888EGon
999000Egon
11111[rootlocalhost ~]# sed -r 1,3{s/egon/XXXX/g;3q} 1.txt
XXXX
111XXXX2
XXXX333XXXX4444
转换命令
[rootlocalhost ~]# cat 2.txt
hello
ok
wo
111
222
link[rootlocalhost ~]# sed -r y/elo/XYZ/ 2.txt
hXYYZ
Zk
wZ
111
222
Yink
四 模式空间与保持空间----了解即可
ed 有两个内置的存储空间 模式空间pattern space 如你所知模式空间用于 sed 执行的正常流程中。该空间 sed 内置的一个缓冲区用来存放、修改从输入文件读取的内容。 保持空间hold space 保持空间是另外一个缓冲区用来存放临时数据。Sed 可以在保持空间和模式空间交换数据但是不能在保持空间上执行普通的 sed 命令。
我们已经讨论过每次循环读取数据过程中模式空间的内容都会被清空然而保持空间的内容则保持不变不会在循环中被删除。
模式空间与保持空间的操作命令
x命令x(exchange) 用于交换模式空间和保持空间的内容h模式空间复制/覆盖到保持空间
H模式空间追加到保持空间g保持空间复制/覆盖到模式空间
G保持空间追加到模式空间n读取下一行到/覆盖到模式空间
N将下一行添加到模式空间d删除pattern space中的所有行并读入下一新行到pattern space中示例:交换文件的行
[rootegon ~]# cat test.txt
1111
2222
3333# 方式1
[rootegon ~]# tac test.txt
3333
2222
1111
[rootegon ~]# # 方式2
思路
# 1、读取文件第一行内容到模式空间进行的操作如下
# 将模式空间内容覆盖到保持空间
# 删除模式空间内容# 2、读取文件第二行内容到模式空间进行的操作如下
# 将保持内容追加到模式空间
# 将模式空间内容覆盖到保持空间
# 删除模式空间内容 # 3、读取文件第三行内容到模式空间进行的操作如下
# 将保持空间内容追加到模式空间实现
sed -r 1h;1d;2G;2h;2d;3G test.txt
或者
sed 1!G;h;$!d test.txt示例
[rootlocalhost ~]# cat e.txt
111
222
333
[rootlocalhost ~]# tac e.txt
333
222
111
[rootlocalhost ~]# sed -r 1h;1d;2G;2h;2d;3G e.txt
333
222
111
五 sed脚本
sed脚本就是写在文件中的一系列sed命令使用-f 选项指定sed脚本文件名需要注意的问题如下
脚本末尾不能有任何多余的空格或文本如果命令不能独占一行就必须以\结尾脚本中不能使用引号除非它们是查找串的一部分反斜杠起到续行的作用
[rootegon ~]# cat sed.sh #永久存储存了多行sed命令相当于多道关卡每读入一行内容将经历一道道关卡
1h;1d;2G;2h;2d;3G
1h;1d;2G;2h;2d;3G[rootegon ~]# sed -r a.txt
1111
2222
3333
[rootegon ~]#
[rootegon ~]# sed -r -f sed.sh test.txt
3333
2222
1111
2222
1111
[rootegon ~]# 六 练习
删除配置文件中用井号#注释的行
sed -r -i /^#/d file.conf
sed -r -i /^[ \t]*#/d file.conf删除配置文件中用双斜杠//注释的行
sed -r -i \c//cd file.conf删除无内容空行
sed -r /^$/d file.conf
sed -r /^[\t]*$/d file.conf
sed -r /^[ \t]*$/d file.conf示例
# 删除#号注释和无内容的空行
sed -r -i /^[ \t]*#/d; /^[ \t]*$/d /etc/vsftpd/vsftpd.conf
sed -r -i /^[ \t]*#|^[ \t]*$/d /etc/vsftpd/vsftpd.conf # 同上追加一行\可有可无有更清晰
sed -r -i $a\chroot_local_userYES /etc/vsftpd/vsftpd.conf 给文件每行加注释
sed -r -i s/^/#/ filename每指定行加注释
sed -r -i 10,$s/^/#/ filename
sed -r 3,$s/^#*/#/ filename # 将行首连续的零个或多个#换成一个#sed中使用外部变量
# var1666
# sed -r 3a$var1 test.txt # 可以不加引号
# sed -r 3a$var1 test.txt # 也可以加引号但注意是双引号而不是单引号因为要用$符号取变量值
# sed -r 3a$var1 test.txt # 也可以sed命令用引起来而变量用注意二者之间不能有空格