linux sed 常用命令学习

时间:2022-12-10 14:56:39

对于经常活跃在linux上的工程师来说,sed是非常重要的工具。

另外,需要了解的是,sed以行为单位在内存中进行读取修改,替换或删除等操作并非真的在源文件上进行,源文件没有任何改变。当然,输出的结果也可以重定向至指定的文件包括源文件里。

sed命令的标准语法是:sed [options] '{command}' [filename]

本文所有操作以如下文本为例。

[root@localhost ~]# cat sed_test.txt 
this is my sed test on sed test!
welcom to use sed to change your work!
I am fbirdzp!
thank you very much!
[root@localhost ~]#

 

#替换操作

使用sed进行替换操作的语法格式为:sed 's/{old value}/{new value}/' 。

命令 描述
sed 's/old/new/’ filename 将文件里每行的第1个"old”替换为"new”
sed 's/old/new/4’ filename 将文件里每行的第4个"old”替换为"new”
sed 's/old/new/g’ filename 全文替换所有的'old’为'new'
sed 's/\(.*\)old/\1new/' filename 替换每行的最后1个"old”为"new”
sed 's/\(.*\)old\(.*old\)/\1new\2/' 替换每行的倒数第2个"old”为"new”
sed 's/old1/new1/g ; s/old2/new2/’ 全文替换old1为new1,每行第一个old2为new2
sed '/old1/s/old2/new/g’ filename 将出现'old1’的行中'old2’全部替换为'new’
sed '/old1/!s/old2/new/g’ filename 全文替换'old2’为'new’,带'old1’的行除外
sed '/old1/ s/old2/new1/' filename 替换"old1”之后的'old2’为'new1'
sed 'n1,n2 s/old/new/’ filename 替换n1至n2行的第1个"old”为"new”

 

 

eg,

1.替换每行第一个sed为vi 
[root@localhost ~]# sed 's/sed/vi/' sed_test.txt 
this is my vi test on sed test!
welcom to use vi to change your work!
I am fbirdzp!
thank you very much!
 
2.全文替换sed为vi
[root@localhost ~]# sed 's/sed/vi/g' sed_test.txt
this is my vi test on vi test!
welcom to use vi to change your work!
I am fbirdzp!
thank you very much!
[root@localhost ~]#
 
3.替换sed_test.txt文件里所有的"sed”为"vi”,每行第一个"test”替换为"work”
[root@localhost ~]# sed 's/sed/vi/g ;s/test/work/' sed_test.txt 
this is my vi work on vi test!
welcom to use vi to change your work!
I am fbirdzp!
thank you very much!
[root@localhost ~]#
 
4.替换sed_test.txt里"my”之后的"sed”为"vi”
[root@localhost ~]# sed '/my/ s/sed/vi/' sed_test.txt 
this is my vi test on sed test!
welcom to use sed to change your work!
I am fbirdzp!
thank you very much!
 
5.替换sed_test.txt里"my”之后的"sed”为"vi”,"on”之后的"sed”为"sar”
[root@localhost ~]# sed '/my/ s/sed/vi/;/on/ s/sed/sar/' sed_test.txt
this is my vi test on sar test!
welcom to use sed to change your work!
I am fbirdzp!
thank you very much!
[root@localhost ~]#
 
6.替换sed_test.txt里第n1行的第一个sed为vi,n2至n3行的"!”为".”
[root@localhost ~]# sed '2 s/sed/vi/;2,3 s/!/./' sed_test.txt 
this is my sed test on sed test!
welcom to use vi to change your work.
I am fbirdzp.
thank you very much!
[root@localhost ~]#


other practise:

sed -n 's/sed/vi/gp' sed_test.txt
sed -n 's/sed/vi/2p' sed_test.txt
sed -n 's/sed/vi/gp; s/test/work/g' sed_test.txt
sed -n 's/sed/vi/gp; s/test/work/gp' sed_test.txt
sed -n '/my/ s/sed/vi/p' sed_test.txt
sed -n '2 s/sed/vi/p; 2,3 s/!/./p' sed_test.txt

注意下面两句可以打印出两行:
sed -n '2 s/sed/vi/p; 2,3 s/!/./p' sed_test.txt
sed -n '/my/ s/sed/vi/p; /on/ s/sed/sar/p' sed_test.txt
this is my vi test on sed test!
this is my vi test on sar test!

-e

sed -ne '/sed/p' -ne '/test/p' sed_test.txt 

 

使用"-e”隔开不同的命令,也可以用分号隔开。一般用";"更为简洁。

#行的显示和删除操作

1.行的显示操作

命令 描述
sed –n 'n1,n2p' filename 显示文件的n1~n2行
sed nq filename 显示文件的前n行
sed –n '/sed/ p' filename 显示文件中带有'sed’的行
sed –n '/sed/ !p' filename 显示文件中不带有'sed’的行

eg,

1.显示sed_test.txt的2到3行
[root@localhost ~]# sed -n '2,3p' sed_test.txt 
welcom to use sed to change your work!
I am fbirdzp!
2.显示带有'sed’的行

[root@localhost ~]# sed -n '/sed/ p' sed_test.txt
this is my sed test on sed test sed one sed zhangp!
welcom to use sed to change your work.
或使用如下命令(删除除带有sed以外的行,剩下的显示出来)
[root@localhost ~]# sed '/sed/ !d' sed_test.txt
this is my sed test on sed test sed one sed zhangp!
welcom to use sed to change your work.
[root@localhost ~]#

3.显示不带有'sed’的行
[root@localhost ~]# sed -n '/sed/ !p' sed_test.txt
I am fbirdzp!
thank you very much.
或使用如下命令(删除带有sed的行)
[root@localhost ~]# sed '/sed/ d' sed_test.txt
I am fbirdzp!
thank you very much.
[root@localhost ~]#


2. 行的删除操作

命令 描述
sed 'n1,n2 d’ filename 删除文件的n1至n2行
sed '/sed/ d' filename 删除文件中所有包含"sed"的行
sed '/sed/ !d' filename 删除文件中除包含"sed"以外的所有行
sed '/^$/ d' filename 删除文件中的空白行
sed '/^th/ d' filename 删除文件中以"th"开头的行
sed '/t!$/ d' filename 删除文件中以"t!"结尾的行
sed '0~nd' 删除文件中n倍数的行

eg,

1.删除以"th”开头的所有行
[root@localhost ~]# cat sed_test.txt
this is my sed test on sed test!
welcom to use sed to change your work!
I am fbirdzp!
thank you very much!
[root@localhost ~]# sed '/^th/ d' sed_test.txt
welcom to use sed to change your work!
I am fbirdzp!
[root@localhost ~]#
 
2.删除以"t!”结尾的所有行
[root@localhost ~]# cat sed_test.txt 
this is my sed test on sed test!
welcom to use sed to change your work.
I am fbirdzp!
thank you very much.
[root@localhost ~]# sed '/t!$/ d' sed_test.txt 
welcom to use sed to change your work.
I am fbirdzp!
thank you very much.
[root@localhost ~]#

 

3.指定的行之后添加或插入一行

第n行之后添加或插入一行特定字符,行号之后跟参数a表示在指定行之后加入一行,参数i表示在指定行之前插入一行。eg,

[root@localhost ~]# sed '2a this is a test' sed_test.txt 
this is my sed test on sed test!
welcom to use sed to change your work.
this is a test --第2行之后添加一行'this is a test '
I am fbirdzp!
thank you very much.
[root@localhost ~]# sed '2i this is a test' sed_test.txt
this is my sed test on sed test!
this is a test --第2行之前插入一行'this is a test '
welcom to use sed to change your work.
I am fbirdzp!
thank you very much.
[root@localhost ~]# sed '$a this is a test' sed_test.txt
this is my sed test on sed test!
welcom to use sed to change your work.
I am fbirdzp!
thank you very much.
this is a test --文件末尾添加一行'this is a test '
[root@localhost ~]#

other practise:
sed 'a insert a new line' sed_test.txt
sed 'i insert a new line' sed_test.txt

 

4.将带有关键词的行修改为指定文本字符

eg,将带有关键词"sed”行修改为"sqlplus as sysdba ”

[root@localhost ~]# sed '/sed/ c sqlplus as sysdba' sed_test.txt 
sqlplus as sysdba
sqlplus as sysdba
I am fbirdzp!
thank you very much.
[root@localhost ~]# cat sed_test.txt
this is my sed test on sed test!
welcom to use sed to change your work.
I am fbirdzp!
thank you very much.
[root@localhost ~]#

 

5.将文件A的n1~n2行写入至文件2中

这是很实用的命令,经常会遇到需要将一个特别大的日志文件中的某部分相关内容择出来输出到一个新的文件里。

1>把文件1的n1至n2行输入至文件2中

sed 'n1,n2 w file2’ file1或是sed -n 'n1,n2p' file1 > file2

eg,

[root@localhost ~]# cat sed_2
cat: sed_2: No such file or directory
[root@localhost ~]# sed -n '2,4 w sed_2' sed_test.txt
[root@localhost ~]# cat sed_2
welcom to use sed to change your work.
I am fbirdzp!
thank you very much.
[root@localhost ~]#
注意:加'-n’参数取消屏幕输出

 

#小技巧

1.增加空行

文件中每一行下面增加一个空行,命令如下:
sed G filename

文件中每一行下面增加两个空行,命令如下:
sed 'G;G' filename

删除文件中原有的空行,并在每一行后增加一个空行,命令如下:
sed '/^$/d;G' filename

删除第一个空白行之前的所有行,命令如下:
sed '1,/^$/ d' filename

2.倒置所有行,第一行成为最后一行

sed -n '1!G;h;$p' filename或是
sed '1!G;h;$!d' filename

3.将行中的字符逆序排列,第一个字成为最后一字

sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' filename

4.每两行合并成一行

sed '$!N;s/\n/ /' filename

5.删除文件中相邻的重复行

sed '$!N; /^\(.*\)\n\1$/!P; D'

 


 

总结

网上学习的时候,发现了很多关于sed的常用命令及一些比较实用小技巧,短短一篇很难总结完,比如一些强制退出、同样输出结果不同的命令会有不同性能表现包括和其他命令的配合使用等等,都需要以后继续学习!


转自:http://fbirdzp.blogbus.com/logs/62624803.html