赞
踩
sed(Stream EDitor)是一个强大而简单的文本解析转换工具,可以读取文本,并根据指定的条件对文本内容进行编辑(删除、替换、添加、移动等),最后输出所有行或者仅输出处理的某些行。sed 也可以在无交互的情况下实现相当复杂的文本处理操作,被广泛应用于 Shell 脚本中,用以完成各种自动化处理任务。
sed 的工作流程主要包括读取、执行和显示三个过程。
1.读取:sed 从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓冲区中(又称模式空间,pattern space)。
2.执行:默认情况下,所有的 sed 命令都在模式空间中顺序地执行,除非指定了行的地址,否则 sed 命令将会在所有的行上依次执行。
3.显示:发送修改后的内容到输出流。再发送数据后,模式空间将会被清空。
其中,“参数”是指操作的目标文件,当存在多个操作对象时用,文件之间用逗号“,”分隔;而scriptfile表示脚本文件,需要用“-f”选项指定,当脚本文件出现在目标文件之前时,表示通过指定的脚本文件来处理输入的目标文件
1.-e或-expression=:表示用指定命令或者脚本来处理输入的文本文件
2.-f或-file=:表示用指定的脚本文件来处理输入的文本文件
3.-h或-help:显示帮助
4.-n、–quite或silent:表示仅显示处理后的结果
5.-i :直接编辑文本文件
操作,用于指定具体的动作行为,通常情况下采用”[n1[,n2]]“操作参数的格式。具体如下:
6.a:增加,在当前行下增加一行指定内容
7.c:替换,将选定行替换为指定内容
8.d:删除,删除选定的行
9.i:插入,在选定行上面插入一行指定内容
10.p:打印,如果同时指定行,表达打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以ASCII码输出。通常与-n一起使用
11.s:替换,替换指定字符
12.y:字符转换
[root@localhost ~]# sed -n 'p' 123.txt
the a
the b
the c
the d
the e
the f
the g
the h
cold
do
wod
wood
111
222
[root@localhost ~]# sed -n '3p' 123.txt
the c
[root@localhost ~]# sed -n '3,6p' 123.txt
the c
the d
the e
the f
[root@localhost ~]# sed -n 'p;n' 123.txt the a the c the e the g cold wod 111 333 444 otten oo xyz Axyz AxyzxyzC [root@localhost ~]# sed -n 'n;p' 123.txt the b the d the f the h do wood 222 555 on ooo xyzxyz AxyzC
[root@localhost ~]# sed -n '2,5{p;n}' 123.txt
the b
the d
[root@localhost ~]# sed -n '3 ,${n;p}' 123.txt
the d
the f
the h
do
wood
222
555
on
ooo
xyzxyz
AxyzC
[root@localhost ~]# sed -n '/o/p' 123.txt
cold
do
wod
wood
otten
on
oo
ooo
[root@localhost ~]# sed -n '4,/o/p' 123.txt
the d
the e
the f
the g
the h
cold
[root@localhost ~]# sed -n '/o/=' 123.txt
9
10
11
12
19
20
21
22
[root@localhost ~]# sed -n '/^o/p' 123.txt
otten
on
oo
ooo
[root@localhost ~]# sed -n '/o$/p' 123.txt
do
oo
ooo
[root@localhost ~]# sed -n '/\<the\>/p' 123.txt
the a
the b
the c
the d
the e
the f
the g
the h
[root@localhost ~]# nl 123.txt | sed '3d'
[root@localhost ~]# nl 123.txt | sed '3,5d'
[root@localhost ~]# nl 123.txt | sed '3d'
//删除不包含cross 的行,用!符号表示取反操作,如’/cross/!d’
[root@localhost ~]# nl 123.txt |sed '/cross/d'
[root@localhost ~]# sed'/^[a-z]/d' 123.txt
[root@localhost ~]# sed '/\.$/d' 123.txt
[root@localhost ~]# sed '/^$/d' 123.txt
sed 's/the/THE/' test.txt
//将每行中的第一个the 替换为 THE
sed 's/l/L/2' test.txt
//将每行中的第 2 个l 替换为L
sed 's/the/THE/g' test.txt
//将文件中的所有the 替换为THE
sed 's/o//g' test.txt
//将文件中的所有o 删除(替换为空串)
sed 's/^/#/' test.txt
//在每行行首插入#号
sed '/the/s/^/#/' test.txt
//在包含the 的每行行首插入#号
sed 's/$/EOF/' test.txt
//在每行行尾插入字符串EOF
sed '3,5s/the/THE/g' test.txt
//将第 3~5 行中的所有the 替换为 THE
sed '/the/s/o/O/g‘ test.txt
//将包含the 的所有行中的o 都替换为 O
sed '/the/{H;d};$G' test.txt
//将包含the 的行迁移至文件末尾,{;}用于多个操作
sed '1,5{H;d};17G' test.txt
//将第 1~5 行内容转移至第 17 行后
sed '/the/w out.file' test.txt
//将包含the 的行另存为文件out.file
sed '/the/r /etc/hostname' test.txt
//将文件/etc/hostname 的内容添加到包含the 的每行以后
sed ’3aNew' test.txt
//在第 3 行后插入一个新行,内容为 New
sed '/the/aNew' test.txt
//在包含the 的每行后插入一个新行,内容为 New
sed '3aNew1\nNew2' test.txt
//在第 3 行后插入多行内容,中间的\n 表示换行
sed '1,5{H;d};17G‘ test.txt
//将第 1~5 行内容转移至第 17 行后
[root@localhost ~]# sed -f opt.list test.txt
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。