sed使用

Posted xinjing-jingxin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sed使用相关的知识,希望对你有一定的参考价值。

sed命令

一、替换标记

s/pattern/replacement/flags

默认情况下只会替换每行的首次出现的内容,如果要替换其他位置需要使用flags

1、不使用flag

[[email protected] tmp]# cat data.txt 
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
[[email protected] tmp]# sed ‘s/test/aaa/‘ data.txt 
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
[[email protected] tmp]# cat data.txt 
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.

2、使用flags/pattern/replacement/flags

4种可用的替换标记:

  • 数字,表明新文本将替换第几处模式匹配的地方;
  • g,表明新文本将会替换所有匹配的文本;
  • p,表明原先行的内容要打印出来;
  • w file,将替换的结果写到文件中。 

1)数字型,比如数字2,替换第2次出现的内容,不会修改原文件

[[email protected] tmp]# sed ‘s/test/hhhh/2‘ data.txt
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.

2)g,替换所有的行,不会修改原文件

[[email protected] tmp]# sed ‘s/test/aaa/g‘ data.txt
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.

3)w file,将替换的结果写到新文件中,不会替换原文件

[[email protected] tmp]# sed ‘s/test/aaa/w aaa.txt‘ data.txt
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.

查看当前目录,多了文件aaa.txt
[[email protected] tmp]# ls
aaa.txt keyring-nBhNRc orbit-gdm pulse-PDnM11f40MzK VMwareDnD
data.txt keyring-o1ipCa orbit-root pulse-tvQCaCozIrxH

4)pp替换标记会打印与替换命令中指定的模式匹配的行

[[email protected] tmp]# cat aaa.txt
this is a aaa of the test script.
this is a sss of the test script.
[[email protected] tmp]# sed ‘s/aaa/test/p‘ aaa.txt
this is a test of the test script.
this is a test of the test script.
this is a sss of the test script.

这通常会和sed-n选项一起使用。
$ cat data5.txt
This is a test line.
This is a different line.


$ sed -n ‘s/test/trial/p‘ data5.txt
This is a trial line.

-n选项将禁止sed编辑器输出。但p替换标记会输出修改过的行。将二者配合使用的效果就是
只输出被替换命令修改过的行。

二、使用地址 

默认情况下,sed命令作用于文本数据的所有行。如果只想将命令作用于特定行或者某些行,则需要使用行寻址。

sed编辑器的2种行寻址方式:

  • 以数字形式表示行区间
  • 用文本模式来过滤出行

[address] command

address {

command1

command2

command3

}

1、数字方式行寻址

[[email protected] tmp]# sed ‘2s/test/hhh/‘ aaa.txt
this is a aaa of the test script.
this is a sss of the hhh script.

 

修改2-4行每行第2次匹配上的内容

[[email protected] tmp]# sed ‘2,4s/test/hhh/2‘ data.txt
this is a test of the test script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.

 

修改第2行至最后一行的匹配的内容

[[email protected] tmp]# sed ‘2,$s/test/hhh/2‘ data.txt
this is a test of the test script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.

 

2、命令组合方式

[[email protected] ~]# sed ‘2,4{        修改2-4行的内容
> s/test/hhh/2                                修改每行的第2次匹配的内容
> s/a test/1111/                              修改每行的第1次匹配的内容
> }‘ /tmp/data.txt
this is a test of the test script.
this is 1111 of the hhh script.
this is 1111 of the hhh script.
this is 1111 of the hhh script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
[[email protected] ~]#

3、删除行

$ sed ‘/number 1/d‘ data6.txt
This is line number 2.
This is line number 3.
This is line number 4.

 

 

 

 

 

 

 

 

 





















































































以上是关于sed使用的主要内容,如果未能解决你的问题,请参考以下文章

linux 特殊符号怎样用sed替换

Sed - 查找和替换 html 代码中的文本(从一种语言到另一种语言)

使用sed替换apache2.conf文件中的代码块内的文本

转载:shell脚本之sed使用----替换变量转义字符

sed 删除

Mac 下如何使用sed -i命令