导航菜单

Linux命令大全-sed命令

陆兴东 2026-04-03 19 浏览 Linux命令

一、简介

sed命令是利用语法或脚本对文本文件进行批量编辑操作的,它能够通过正则表达式或关键词对文本文件进行批量编辑,让重复性的工作简单化,以达到节省处理文本的时间。

二、语法

**语法:**set 参数 文本文件

**参数说明:**

-e<script>或--expression=<script>:以选项中指定的script来处理输入的文本文件。

-f<script文件>或--file=<script文件>:以选项中指定的script文件来处理输入的文本文件。

-h或--help:显示帮助。

-n或--quiet或--silent:仅显示script处理后的结果。

-V或--version:显示版本信息。

**动作说明:**

a :新增, a的后面可以接字符串,而这些字符串会在新的一行出现(目前的下一行)。

c :取代, c的后面可以接字符串,这些字符串可以取代n1,n2 之间的行。

d :删除,因为是删除啊,所以d后面通常不接任何东东。

i :插入, i的后面可以接字符串,而这些字符串会在新的一行出现(目前的上一行)。

p :打印,亦即将某个选择的数据印出。通常p会与参数sed -n一起运行。

s :取代,可以直接进行取代的工作哩!通常这个s的动作可以搭配正则表达式,例如 1,20s/old/new/g 就是啦。

三、 实战

在操作前,我们先创建一个测试文件,用来辅助我们完成sed命令的使用。

[root@app01 ~]# cat testfile //查看testfile中的内容

hello linux!

This is a sed testfile!

sed test

Google

Baidu

testfile

**1、在指定的某一行后面添加内容,并把结果标准输出**

[root@app01 ~]# sed -e '3a\3linetest' testfile

hello linux!

This is a sed testfile!

sed test

3linetest

Google

Baidu

testfile

**2、删除某行到某行的内容**

[root@app01 ~]# cat -n testfile|sed '5,6d'

     1  hello linux!

     2  This is a sed testfile!

     3  sed test

     4  Google

sed的动作为5,6d,其中d是删除的意思,因为删除了5-6行,所以显示出来的数据就没有5-6行的内容了,另外,原本应该是要加上-e参数才对,但没有-e也是可以的,同时也要注意的是,sed后面接的动作,请务必以两个单引号引住!

**3、只删除某一行数据(如只删除第1行)**

[root@app01 ~]# cat -n testfile|sed "1d"

     2  This is a sed testfile!

     3  sed test

     4  Google

     5  Baidu

     6  testfile

**4、删除某一行到最后一行的内容(如删除4行到最后一行的内容)**

[root@app01 ~]# cat -n testfile|sed '4,$d'

     1  hello linux!

     2  This is a sed testfile!

     3  sed test

**5、在指定行后加上内容(如在第2行后(即第3行)加testname)**

[root@app01 ~]# cat -n testfile|sed -e '2a testname'

     1  hello linux!

     2  This is a sed testfile!

testname

     3  sed test

     4  Google

     5  Baidu

     6  testfile

如果是想加在某一行前面,如下所示:

[root@app01 ~]# cat -n testfile|sed -e '2i testname'

     1  hello linux!

testname

     2  This is a sed testfile!

     3  sed test

     4  Google

     5  Baidu

     6  testfile

```


如果在某一行后面加2行以上的内容,如下所示:

[root@app01 ~]# cat -n testfile|sed -e '2a testname.....\

> aaaa'

     1  hello linux!

     2  This is a sed testfile!

testname.....

aaaa

     3  sed test

     4  Google

     5  Baidu

     6  testfile

每一行之间都必须要以反斜杠“\\”来做新行标记。上面的例子中,我们可以发现在第2行的内容最后面就有”\\“存在。

**6、将某行到某行的内容,替换成其它内容**


```

[root@app01 ~]# cat -n testfile |sed -e '2,3c 2-3newline'

     1  hello linux!

2-3newline

     4  Google

     5  Baidu

     6  testfile

```


通过以上方法我们就能够将数据整行取代或替换了。


**7、列出某行到某行的内容**


```

[root@app01 ~]# cat -n testfile|sed -n '2,4p'

     2  This is a sed testfile!

     3  sed test

     4  Google

```


仅列出了2-4行的内容,其它行内容已被过滤。


**8、搜索包含某个关键字的行**


```

[root@app01 ~]# cat -n testfile|sed -n '/oo/p'

     4  Google

```


**9、删除包含某个关键字的行,并显示出其它行**


```

[root@app01 ~]# cat -n testfile|sed -e '/oo/d'

     1  hello linux!

     2  This is a sed testfile!

     3  sed test

     5  Baidu

     6  testfile

```


**10、搜索包含某个关键字的行,并替换成其它内容**


```

[root@app01 ~]# sed -e 's/oo/aaaaaaaaaaaaa/g' testfile

hello linux!

This is a sed testfile!

sed test

Gaaaaaaaaaaaaagle

Baidu

testfile

```


“g”标识符表示全局查找替换,使sed对文件中所有符合的字符串都被替换,修改后内容会到标准输出,不会修改原文件,如果想修改原文件,可使用“-i”参数。如下所示:


```

[root@app01 ~]# sed -i 's/oo/aaaaaaaaaaaa/g' testfile

[root@app01 ~]# cat testfile

hello linux!

This is a sed testfile!

sed test

Gaaaaaaaaaaaagle

Baidu

testfile

```


以上Google中的“oo”被替换成了“aaaaaaaaaaaa”。


**11、一次完成多次操作**


以上命令操作,用了2个-e,表示多点编辑,第一个编辑命令是删除testfile第5行到6行数据,第二条命令是搜索“aaaaaaaaaaaa”替换为“oo”。


**注:**sed的“-i”这个参数可以直接修改文件内容,不必使用管道或让数据重新导向到新文件,需要注意的,由于这个动作会直接修改到原始的文件,所以请你千万不要随便拿系统配置或日常工作中比较重要的文件来测试,如果确实有需要,修改前一定要做好文件的备份,以防出现问题,可以随时恢复。