第十八章 文本处理流编辑器:sed命令

第十八章 文本处理流编辑器:sed命令

名词解释

sed 是一种流编辑器,它是文本处理常用到的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。sed主要用来自动编辑一个或多个文件,简化多文件的反复操作,编写转换程序等。

sed的选项、命令、替换标记

命令格式

sed [options] 'command' file(s)
sed [options] -f scriptfile file(s)

选项

  • -e <script>或--expression=<script>:以选项中的指定script来处理输入的文本文件;
  • -f<script>或--file=<script文件>:以选项中的指定的script文件来处理输入的文本文件;
  • -h或--help :显示帮助;
  • -i 直接在文档里修改内容,而不打印到终端;如果不加i,默认并不会修改文件,只是将替换的内容打印到终端
  • -n或--quiet或--silent:仅显示script处理后的结果;
  • -v或--version:显示版本信息。

参数

文件:指定待处理的文本文件列表

sed命令

a\ :在当前行下面插入文本
i\ :在当前行上面插入文本
c\ :把选定的行改为新的文本
d :删除,删除选择的行
D :删除模板块的第一行
s :替换指定字符
h :拷贝模板块的内容到内存中的缓冲区
H :追加模板块的内容到内存中的缓冲区
g :获得内存缓冲区的内容,并替代当前模板块中的内容
G :获得内存缓冲区的内容,并追加到当前模板块文本的内容
l :列表不能打印字符的清单。
n :读取下一个输入行,ongoing下一个命令处理新的行 而不是用第一个命令
N :追加下一个输入行到模板块后面,并在二者间嵌入一个新行,改变当前行号码。
p 打印模板块的行
P(大写) 打印模板块的第一行
q 退出sed
b lable 分支到脚本中带有标记的地方,如果分支不存在,则分支到脚本的末尾。
r file 从file中读取
t lable if分支,从最后一行开始,条件一旦满足或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾。
w file 写并追加模板块到file末尾
W file 写并追加模板块的第一行到file末尾。
!表示后面的命令对所有没有被选定的行发生作用。
= 打印当前行号码
# 把注释扩展到下一个换行符以前

sed替换标记

  • g 表示行内全面替换;表示替换每行的所有符合条件的字符串;如果不加g,默认只替换每行的第一个符合条件的字符串。
  • p 表示打印行;
  • w 表示把行写入一个文件;
  • x 表示互换模板块中的文本和缓冲区中的文本;
  • y 表示把一个字符翻译为另外的字符(但是不用于正则表达式)
  • \1 子串匹配标记
  • & 已匹配字符串标记

sed元字符集

^ :匹配行开始,如:/^sed/ 匹配所有以sed开头的行;
$ : 匹配行结束,如:/sed$/ 匹配所有以sed结尾的行;
. :匹配一个非换行符的任意字符,如:/s.d/匹配s后接一个任意字符,最后是d;
* :匹配0个或多个字符,如:/*sed/匹配所有模板是一个或多个空格后紧跟着sed的行;
[]:匹配一个指定范围内的字符,如/[sS]ed/ 匹配sed和Sed;
[^]:匹配一个不再指定范围内的字符,如:/[^A-RT-Z]ed/ 匹配不包含A-R和T-Z的一个字母开头,紧跟ed的行;
\(..\) :匹配子串,保存匹配的字符,如:s/\(love\)able/\ers, loveable 被替换成lovers;
& :保存搜索字符用来替换其他字符,如:s/love/**&**/,love编程**love**;
\< :匹配单词的开始,如:/\<LOVE/ 匹配包含以love开头的单词的行;
\> :匹配单词的结束,如:/love\> 匹配包含以love结束的单词的行;
x\{m\} :重复字符x,m次,如:/0\{5\}/ 匹配包含5个0的行;
x\{m,\} :重复字符x,至少m次,如:/0\{5\}/ 匹配至少有5个0的行;
x\{m,n\} :重复字符x,至少m次,不多于n次,如:/0\{5,10\} 匹配5-10个0的行。

sed用法实例

替换操作:s命令

[root@ceshi sed]# cat file 
book1book2book3
book1book2book3
book1book2book3
test1test2test3
test1test2test3

替换文本中的字符串:

[root@ceshi sed]# sed 's/book/books/' file 
books1book2book3
books1book2book3
books1book2book3
test1test2test3
test1test2test3

-n选项和p命令 一起使用 表示只打印那些发生替换的行:

[root@ceshi sed]# sed -n 's/test/TEST/p' file 
TEST1test2test3
TEST1test2test3

直接编辑文件选项-i ,会匹配file文件中的每一行的第一个book替换为books:

[root@ceshi sed]# sed -i 's/book/books/' file 
[root@ceshi sed]# cat file 
books1book2book3
books1book2book3
books1book2book3
test1test2test3
test1test2test3

全面替换标记g

使用后缀/g标记 替换每一行中的所有匹配字符:

[root@ceshi sed]# sed -i 's/book/books/g' file 
[root@ceshi sed]# cat file 
bookss1books2books3
bookss1books2books3
bookss1books2books3
test1test2test3
test1test2test3

当需要从第N出匹配开始替换时,可以使用/Ng:

[root@ceshi sed]# sed 's/books/BOOK/2g' file 
bookss1BOOK2BOOK3
bookss1BOOK2BOOK3
bookss1BOOK2BOOK3
test1test2test3
test1test2test3

定界符

以上命令中字符/在sed中作为定界符使用,也可以使用任意的定界符:

[root@ceshi sed]# sed 's|test|TEST|g' file 
bookss1books2books3
bookss1books2books3
bookss1books2books3
TEST1TEST2TEST3
TEST1TEST2TEST3

[root@ceshi sed]# sed 's#test#TEST#g' file 
bookss1books2books3
bookss1books2books3
bookss1books2books3
TEST1TEST2TEST3
TEST1TEST2TEST3

定界符出现在样式内部时,需要进行转义:

#将 /bin/ 替换成 /user/local/

[root@ceshi sed]# echo /bin/ | sed 's/bin/user\/local/g'  
/user/local/

[root@ceshi sed]# echo /bin/ | sed 's/\/bin/\/user\/local/g'         
/user/local/

删除操作:d命令

删除空白行

sed '/^$/d' file

删除文件的第2行:

sed '2d' file

删除文件的第2行到末尾所有行:

sed '2,$d' file

删除文件最后一行:

sed '$d' file

删除文件中所有开头是test的行:

sed '/^test/d' file

已匹配字符串标记&

正则表达式 \w+ 匹配每一个单词,使用[&]替换它,&对应于之前所匹配到的单词:

[root@ceshi sed]# echo this is a test line | sed 's/\w\+/[&]/g'
[this] [is] [a] [test] [line]

所有以192.168.0.1开头的行都会被替换成它自己加localhost:

[root@ceshi sed]# cat ip.txt 
192.168.0.1
192.168.0.2
192.168.0.3

[root@ceshi sed]# sed 's/^192.168.0.1/&locaohost/g' ip.txt 
192.168.0.1locaohost
192.168.0.2
192.168.0.3

子串匹配标记\1

匹配给定样式的其中一部分:

[root@ceshi sed]# echo this is digit 7 in a number | sed 's/digit \([0-9]\)/\1/'
this is 7 in a number

命令中digit 7 被替换成了7;样式匹配到的子串是7,
\(..\) 用于匹配子串,对于匹配到的第一个子串标记为\1,依次类推匹配到第二个结果就是\2,例如:

[root@ceshi sed]# echo aaa BBB | sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/'
BBB aaa

# +号前需要加转义符;

book被标记为1,所有books会被替换成book@@,并打印出来:

[root@ceshi sed]# cat file 
books1books2books3
books1books2books3
books1books2books3

[root@ceshi sed]# sed -n 's/\(book\)s/\1@@/gp' file 
book@@1book@@2book@@3
book@@1book@@2book@@3
book@@1book@@2book@@3

组合多个表达式

sed '表达式' | sed '表达式'
等价于:
sed '表达式;表达式'

引用

sed表达式可以使用单引号来引用,但是如果表达式内部包含变量字符串,就需要使用双引号:

test=hello
echo hello WORLD | sed "s/$test/HELLO/"
HELLO WORLD

选定行的范围:,逗号

所有在模板test和check所确定的范围内的行都被打印:

[root@ceshi sed]# cat file 
bookss1books2books3
bookss1books2books3
bookss1books2books3
check1check2check3
check4check5check6
test1test2test3
test1test2test3

[root@ceshi sed]# sed -n '/check/,/test/p' file 
check1check2check3
check4check5check6
test1test2test3

[root@ceshi sed]# sed -n '/test/,/check/p' file  
test1test2test3
test1test2test3

得出:
当check所在的行在test前面,那么就打印check和test所在的行,并且不打印重复一样的行;
当test所在的行在check后面,那么只打印test所在的行,并且打印重复的行。

打印从第4行开始到第一个包含以check开始的行之间的所有行:

[root@ceshi sed]# sed -n '4,/^check/p' file 
check1check2check3
check4check5check6

对于模板check和test之间的行,每行的末尾用字符串aaa bbb替换:

[root@ceshi sed]# sed -i '/check/,/test/s/$/aaa bbb/g' file 
[root@ceshi sed]# cat file 
bookss1books2books3
bookss1books2books3
bookss1books2books3
check1check2check3aaa bbb
check4check5check6aaa bbb
test1test2test3aaa bbb
test4test5test6

多点编辑:e命令

-e选项允许在同一行里执行多条命令:

[root@ceshi sed]# sed -e '1,5d' -e 's/test/check/' file 
check1test2test3aaa bbb
check4test5test6

上面sed表达式的第一条命令是:删除第1至5行,第二条命令是:check替换成test。
命令的顺序对结果有影响。如果两个命令都是替换命令,那么第一个替换命令将影响第二个替换命令的结果。

和-e等价的命令是--expression:

sed --expression='s/test/check/' --expression='/love/d' file

第一条命令是:test替换成check
第二条命令是:删除love

从文件读入:r命令

file里的内容读取进来,显示在与test匹配的行后面,如果匹配多行,则file的内容将显示在所有匹配行的下面:

[root@ceshi sed]# cat test   #查看test文件的内容
aaa
bbb
test
ccc
ddd

[root@ceshi sed]# sed '/test/r file' test 
#先匹配test文件里 test所在的行内容以及之前打印出来
aaa
bbb
test
#再把file里边的内容打印出来
bookss1books2books3
bookss1books2books3
bookss1books2books3
check1check2check3aaa bbb
check4check5check6aaa bbb
test1test2test3aaa bbb
test4test5test6
#再接着打印test文件里剩下的内容
ccc
ddd

写入文件:w命令

在example中所有包含test的行都被写入file里:

[root@ceshi sed]# cat example 
test01
test02
aaaa
[root@ceshi sed]# sed -n '/test/w file' example
[root@ceshi sed]# cat file 
test01
test02

追加(行下):a\ 命令

将this is a test line 追加到 以test开头的行后面:

[root@ceshi sed]# sed '/^test/a\this is a test line' file 
test01
this is a test line
test02
this is a test line

在file文件第1行之后插入this is a test line:

[root@ceshi sed]# sed -i '1a\this is a test line' file  
[root@ceshi sed]# cat file 
test01
this is a test line
test02

插入(行上):i\ 命令

将this is a test line 追加到file开头的行前面

[root@ceshi sed]# sed -i '/^test/i\this is a test line' file 
[root@ceshi sed]# cat file 
this is a test line
test01
this is a test line
this is a test line
test02

在file文件第3行之前插入hello world:

[root@ceshi sed]# sed -i '3i\hello world' file 
[root@ceshi sed]# cat file 
this is a test line
test01
hello world
this is a test line
this is a test line
test02

下一个:n命令

如果test被匹配,则移动到匹配行的下一行,替换这一行的aa,变成bb,并打印改行,然后继续:

[root@ceshi sed]# sed  '/test/{ n; s/hello/HELLO/; }' file   
this is a test line
test01
hello world
this is a test line     #在这里匹配到了test
HELLO world 
this is a test line     #在这里也匹配到了
test02                 #但是这一行没有hello
hello world
this is a test line

#结果只有一个hello符合条件

变形:y命令

把1-5行内所有test转变为大写,注意:正则表达式元字符不能使用这个命令:

[root@ceshi sed]# cat file 
this is a test line
test01
hello world
this is a test line
hello world
this is a test line
test02
hello world
this is a test line

[root@ceshi sed]# sed '1,5y/test/TEST/' file 
ThiS iS a TEST linE
TEST01
hEllo world
ThiS iS a TEST linE
hEllo world
this is a test line
test02
hello world
this is a test line

退出:q命令

打印完2行后,退出sed:

[root@ceshi sed]# sed '2q' file 
this is a test line
test01

保持和获取:h命令和G命令

互换模式空间和保持缓冲区的内容。也能是把包含test与check的行互换:

#把test文件里的bbb换成aaa(并不修改文件,只是打印到终端显示)
[root@ceshi sed]# cat test 
aaa
bbb
test
ccc
ddd
[root@ceshi sed]# sed -e '/aaa/h' -e '/bbb/x' test 
aaa
aaa
test
ccc
ddd

脚本script file

sed脚本时一个sed的命令清单,启动sed时,以-f选项引导脚本文件名。sed对于脚本中输入的命令非常挑剔,在命令的末尾不能有任何空白或文本,如果在一行中有多个命令,要用分号分隔。以#开头的行为是注释行,且不能跨行。

sed [options] -f script_file file(s)

打印奇数行或偶数行

方法1:

sed -n 'p;n' test.txt   #奇数行
sed -n 'n;p' test.txt   #偶数行

方法2:

sed -n '1~2p' test.txt  #奇数行
sed -n '2~2p' test.txt  #偶数行

打印匹配字符串并且打印下一行

方法1:
[root@ceshi sed]# grep -A 1 test test 
test
ccc

方法2:
[root@ceshi sed]# sed -n '/test/{n;p}' test 
ccc

方法3:
[root@ceshi sed]# awk '/test/{getline; print}' test 
ccc

猜你喜欢

转载自blog.51cto.com/506554897/2146234
今日推荐