首页 Linux sed 命令
文章
取消

Linux sed 命令

sed 命令是流编辑器,处理时,把当前处理的行存储到临时缓冲区中,称为“模式空间”(pattern space),接着用 sed 命令处理缓冲区中的内容,处理完成后,把缓冲区的内容输出到终端,然后处理下一行,这样不断重复,直到文件末尾。文件内容不会改变,除非使用重定向存储输出。

sed 命令处理文本时以行为单位。

sed 常用内部命令

创建两个文本文件,test.txt,内容如下:

this is first line. this is second line. this is third line.

this is fifth line My favorite os is linux

test2.txt,内容如下:

Apple is my favorite,Huawei is his favorite,Apple is her favorite. Joe’s favorite is Apple.

a,追加

append,追加操作

在第5行下面增加一行

1
2
3
4
5
6
7
8
[uphie@node01 ~]$ sed '5a this is sixth line.' test.txt
this is first line.
this is second line.
this is third line.

this is fifth line
this is sixth line.
My favorite os is linux

在以 fifth line 结尾的行下面添加 Happy New Year

1
2
3
4
5
6
7
8
[uphie@node01 ~]$ sed '/fifth line$/a Happy New Year' test.txt
this is first line.
this is second line.
this is third line.

this is fifth line
Happy New Year
My favorite os is linux

i,插入

insert,插入操作

在第六行前插入新行:

1
2
3
4
5
6
7
8
[uphie@node01 ~]$ sed '6i this is 6th line' test.txt
this is first line.
this is second line.
this is third line.

this is fifth line
this is 6th line
My favorite os is linux

d,删除

delete,删除操作

利用正则 ^$ 删除空行:

1
2
3
4
5
6
[uphie@node01 ~]$ sed '/^$/d' test.txt
this is first line.
this is second line.
this is third line.
this is fifth line
My favorite os is linux

删除第 n 行:

1
2
3
4
5
6
[uphie@node01 ~]$ sed '1d' test.txt
this is second line.
this is third line.

this is fifth line
My favorite os is linux

删除第 m 到 n 行:

1
2
3
4
[uphie@node01 ~]$ sed '1,3d' test.txt

this is fifth line
My favorite os is linux

r,读文件

读取 test2.txt,将内容显示在所有以 line. 结尾的行后面

1
2
3
4
5
6
7
8
9
10
11
12
13
[uphie@node01 ~]$ sed '/line\.$/r test2.txt' test.txt
this is first line.
Apple is my favorite,Huawei is his favorite,Apple is her favorite.
Joe's favorite is Apple.
this is second line.
Apple is my favorite,Huawei is his favorite,Apple is her favorite.
Joe's favorite is Apple.
this is third line.
Apple is my favorite,Huawei is his favorite,Apple is her favorite.
Joe's favorite is Apple.

this is fifth line
My favorite os is linux

w,写文件

将 test.txt 中所有以 My 开头的行写入到文件 test3.txt 中:

1
2
3
[uphie@node01 ~]$ sed -n '/^My/w test3.txt' test.txt
[uphie@node01 ~]$ cat test3.txt
My favorite os is linux

s,替换

替换

对于这样内容的测试文件:

形式为 sed 's/待替换文本/目标文本/选项' file

将每行的首个 Apple 替换为 苹果

1
2
3
[uphie@node01 ~]$ sed 's/Apple/苹果/' test2.txt
苹果 is my favorite,Huawei is his favorite,Apple is her favorite.
Joe's favorite is 苹果.

将每行的所有 Apple 替换为 苹果

1
2
3
[uphie@node01 ~]$ sed 's/Apple/苹果/g' test2.txt
苹果 is my favorite,Huawei is his favorite,苹果 is her favorite.
Joe's favorite is 苹果.

每行的从第二个开始将所有 Apple 替换为 苹果

1
2
3
[uphie@node01 ~]$ sed 's/Apple/苹果/2g' test2.txt
Apple is my favorite,Huawei is his favorite,苹果 is her favorite.
Joe's favorite is Apple.

使用正则匹配到的结果使用&表示:

1
2
[uphie@node01 ~]$ echo I love linux | sed 's/\w\+/[&]/g'
[I] [love] [linux]

使用正则匹配到的结果的子串按顺序使用序号表示,如 \1\2

1
2
[uphie@node01 ~]$ echo I love linux os,not windows os | sed 's/\([a-z]\+\) os/\1/g'
I love linux,not windows

l,输出不可打印字符

打印数据流中文本和不可打印的 ASCII 字符,如结束符、制表符

1
2
3
4
5
6
7
8
9
10
11
12
13
[uphie@node01 ~]$ sed 'l' test.txt
this is first line.$
this is first line.
this is second line.$
this is second line.
this is third line.$
this is third line.
$

this is fifth line$
this is fifth line
My favorite os is linux$
My favorite os is linux

p,打印

print,打印输出

查看文件全部内容,有2种方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[uphie@node01 ~]$ sed ' ' test.txt
this is first line.
this is second line.
this is third line.

this is fifth line
My favorite os is linux
[uphie@node01 ~]$ sed -n 'p' test.txt
this is first line.
this is second line.
this is third line.

this is fifth line
My favorite os is linux

查看第 n 行

1
2
[uphie@node01 ~]$ sed -n '2p' test.txt
this is second line.

查看第 m 到 n 行

1
2
3
[uphie@node01 ~]$ sed -n '1,2p' test.txt
this is first line.
this is second line.

查看最后一行

1
2
[uphie@node01 ~]$ sed -n '$p' test.txt
My favorite os is linux

q,退出

quit,退出

打印前3行后退出

1
2
3
4
[uphie@node01 ~]$ sed '3q' test.txt
this is first line.
this is second line.
this is third line.

y,变形

将所有的 o 替换为 Os 替换为 S

1
2
3
4
5
6
7
[uphie@node01 ~]$ sed 'y/os/OS/' test.txt
thiS iS firSt line.
thiS iS SecOnd line.
thiS iS third line.

thiS iS fifth line
My favOrite OS iS linux

=,打印行号

打印行号

1
2
3
4
5
6
7
8
9
10
11
12
13
[uphie@node01 ~]$ sed '=' test.txt
1
this is first line.
2
this is second line.
3
this is third line.
4

5
this is fifth line
6
My favorite os is linux

变量引用

定义变量 brand,将所有的 Apple 换成 OPPO,这时要使用双引号:

1
2
3
4
[uphie@node01 ~]$ brand=Apple
[uphie@node01 ~]$ sed "s/$brand/OPPO/g" test2.txt
OPPO is my favorite,Huawei is his favorite,OPPO is her favorite.
Joe's favorite is OPPO.

选定行的范围

打印包含 first 和 third 的行范围内的行:

1
2
3
4
[uphie@node01 ~]$ sed -n '/first/,/third/p' test.txt
this is first line.
this is second line.
this is third line.

打印从第2行开始,一直到包含 third 的行:

1
2
3
[uphie@node01 ~]$ sed -n '2,/third/p' test.txt
this is second line.
this is third line.

second line.third line.结尾的行范围内的行的开头添加 Attention

1
2
3
4
5
6
7
[uphie@node01 ~]$ sed '/second line\.$/,/third line\.$/s/^/Attention /' test.txt
this is first line.
Attention this is second line.
Attention this is third line.

this is fifth line
My favorite os is linux
本文由作者按照 CC BY 4.0 进行授权