Linux 中【sed】 命令快速参考

IOT物联网小镇

共 7026字,需浏览 15分钟

 · 2022-04-10

目录

  • 一、sed 命令简介

    • 输入流

    • 输出流

  • 二、sed 命令格式

    • 常用选项

    • 常用命令

    • 正则表达式

  • 三、使用举例

    • 内容显示

    • 内容替换

    • 内容添加

    • 行删除

    • 行替换

    • 内容搜寻

    • 级联操作




GNU 官方文档

一、sed 命令简介

sed命令的全称是:stream editor(数据流编辑器),言下之意就是对流式数据进行处理。

主要使用场景:对文本中的特定字符串进行替换或者删除

它每次从输入流中读取一行内容,去掉末尾的换行符之后,放在内部的一个缓冲区中,

然后根据命令参数对缓冲区中数据进行处理,最后把处理后数据输出到输出流中。

输入流可以是:终端输入内容、管道、文件;

输出流可以是:终端输出、文件。

1. 输入流:终端输入

$ sed 's/my/your/g' -

这条指令意思是:把输入字符中的字符串 "my" 替换成 "your"。

最后的 - 表示sed命令将会读取用户的输入内容,用户输入完一行内容(按下回车):

this is my book

此时,sed就会接收到这行内容,然后进行处理,最后把处理结果输出到终端屏幕上:

this is your book

当处理完这行内容之后,继续等待读取用户的下一行输入内容。

2. 输入流:管道

$ echo "this is my book" | sed 's/my/your/g'
this is your book

echo 命令的输出结果,经过管道被sed命令读取,然后进行处理,最后输出到终端屏幕上。

3. 输入流:文件

测试文件file.txt中内容如下:

This is my book
This is my box

把文件中字符串my替换成your

$ sed 's/my/his/g' file.txt
This is your book
This is your box

上面这条指令把处理结果输出到终端屏幕上,对file.txt文件本身并没有任何改动。

如果想直接对文件进行操作,那么就需要加上一个选项:-i,完整指令如下:

$ sed -i 's/my/his/g' file.txt

4. 输出流

上面的几个示例中, sed命令直接把输出流(处理结果)输出到终端屏幕上。

除此之外,sed还可以把输出流通过管道传递给其它指令,例如:

$ echo "this is my book" | sed 's/my/your/g' | wc
1 4 18

还可以让sed把处理结果重导向到一个文件中:

$ echo "this is my book" | sed 's/my/your/g' > test.txt
$ cat test.txt
this is your book

或者是直接在指令中通过 w标志指定输出文件:

$ echo "this is my book" | sed 's/my/your/gw test.txt'
$ cat test.txt
this is your book

二、sed 命令格式

$ sed [选项] [命令] [输入文件]

常用选项:

-i: 直接对文件内容进行修改;

-e: 直接在指令列模式上进行动作编辑;

-f: 执行一个文件内的 sed 命令;

-n: 使用安静模式;

-r: 使用扩展的正则表达式来进行内容匹配;

常用命令:

a: 在当前选择行的后面追加一行;

i: 在当前选择行的前面追加一行;

c: 把当前选择行内容进行替换;

d: 删除当前选择的行;

s: 对当前行中选择的字符串进行替换;

g: 在当前选择行中进行全部替换;

p: 打印选择的行;

支持的正则表达式:

^: 行首定位符;

$: 行尾定位符;

.: 匹配除了换行符之外的其他任意一个字符;

+: 匹配前面一个字符重复 1 次或多次;

*: 匹配前面一个字符重复 0 次或多次;

{i}: 匹配前面一个字符重复 i 次;

{i,}: 匹配前面一个字符重复至少 i 次;

{i,j}: 匹配前面一个字符重复 i ~ j 次;

\n: 匹配换行符

三、使用举例

为了便于演示,以下示例中的输入流来自文件,输出到终端屏幕上。

测试文件file.txt内容:

line1: This is my book, a nice book.
line2: That is your book, a nice book.
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!

如果需要直接对文件进行修改的话,加上-i选项即可。

内容显示

示例:显示文件中第2行:

$ sed -n '2p' file.txt 
line2: That is your book, a nice book.

示例:显示文件中第2 ~ 4行:

$ sed -n '2,4p' file.txt 
line2: That is your book, a nice book.
line3: I am 30 years old.
line4: You are 30 years old.

示例:显示文件中除了2 ~ 4行之外的其它行:

$ sed '2,4d' file.txt
line1: This is my book, a nice book.
line5: Welcome to www.linuxbar.net!

内容替换

示例:把每一行中的book替换成picture:

$ sed 's/book/picture/' file.txt 
line1: This is my picture, a nice book.
line2: That is your picture, a nice book.
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!

可以看到:第1、2行中只有第一个book字符串被替换了,后面的那个没有被替换。

示例:如果要全部替换,需要加上g标志,如下:

$ sed 's/book/picture/g' file.txt
line1: This is my picture, a nice picture.
line2: That is your picture, a nice picture.
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!

内容替换的格式是:sed 's/old/new/g' flie,使用斜杠/进行分割,在处理文件路径时就显得很麻烦,例如:

示例:把路径分隔符进行替换:

$ echo "/home/tmp" | sed 's/\//\\/g'
\home\tmp

此时,可以使用除了换行符之外的其他任意字符来替代,例如:

$ echo "/home/tmp" | sed 's#/#\\#g'
\home\tmp

\:因为反斜杠自身是一个特殊的字符,所以需要进行转义。

另外一个问题:sed把每一行都输出到屏幕上了,包括没有匹配的那些行。

如何只显示处理的那些行呢?

示例:把每一行中的book替换成picture: ,但是只显示处理的行:

$ sed -n 's/book/picture/gp' file.txt
line1: This is my picture, a nice picture.
line2: That is your picture, a nice picture.

示例:将每一行中的第2book替换成picture:

$ $ sed -n 's/book/picture/2p'  file.txt
line1: This is my book, a nice picture.
line2: That is your book, a nice picture.

示例:将2 ~ 3行中的book替换成picture:

$ sed  '2,3s/book/picture/g'  file.txt
line1: This is my book, a nice book.
line2: That is your picture, a nice picture.
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!

示例:将 2 ~ 最后一行中的30替换成100:

$ sed  '2,$s/30/100/g'  file.txt
line1: This is my book, a nice book.
line2: That is your book, a nice book.
line3: I am 100 years old.
line4: You are 100 years old.
line5: Welcome to www.linuxbar.net!

示例:找出包含字符years的那些行,将这些行中的30替换成100:

$ sed  '/years/s/30/100/g'  file.txt 
line1: This is my book, a nice book.
line2: That is your book, a nice book.
line3: I am 100 years old.
line4: You are 100 years old.
line5: Welcome to www.linuxbar.net!

示例:找出满足 Th[is|at] 模式的那些行,将这些行中的book替换成picture:

$ sed  '/Th[is|at]/s/book/picture/g'  file.txt
line1: This is my picture, a nice picture.
line2: That is your picture, a nice picture.
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!

示例:找出包含wwwnet的那些行,把bar替换成BAR:

$ sed '/www/,/net/ s/bar/BAR/' file.txt 
line1: This is my book, a nice book.
line2: That is your book, a nice book.
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxBAR.net!

示例:忽略大小写,把每行中的 OLD 替换成###:

$ sed 's/OLD/###/i'  file.txt
line1: This is my book, a nice book.
line2: That is your book, a nice book.
line3: I am 30 years ###.
line4: You are 30 years ###.
line5: Welcome to www.linuxbar.net!

示例:从第2行开始一直到包含am行之间的所有行中,把30替换成100:

$ sed '2,/am/s/30/100/' file.txt 
line1: This is my book, a nice book.
line2: That is your book, a nice book.
line3: I am 100 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!

示例:从包含am开始的那行开始一直到最后一行,把其中的数字替换成#:

$ sed '/am/,$ s/[0-9]/#/g' file.txt 
line1: This is my book, a nice book.
line2: That is your book, a nice book.
line#: I am ## years old.
line#: You are ## years old.
line#: Welcome to www.linuxbar.net!

示例:把每一行中的小写字母a替换成大写字符A, 小写字母b替换成大写字符B:

$ sed  'y/ab/AB/'  file.txt
line1: This is my Book, A nice Book.
line2: ThAt is your Book, A nice Book.
line3: I Am 30 yeArs old.
line4: You Are 30 yeArs old.
line5: Welcome to www.linuxBAr.net!

内容添加

示例:在第2行的前面添加一行内容:

$ sed '2i\new line: hello' file.txt 
line1: This is my book, a nice book.
new line: hello
line2: That is your book, a nice book.
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!

示例:在第2行后面面添加一行内容:

$ sed '2a\new line: hello' file.txt 
line1: This is my book, a nice book.
line2: That is your book, a nice book.
new line: hello
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!

行删除

示例:删除2 ~ 4行:

$ sed '2,4d' file.txt 
line1: This is my book, a nice book.
line5: Welcome to www.linuxbar.net!

示例:删除包含字符串book的行:

$ sed '/book/d' file.txt 
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!

示例:删除不包含字符串book的行:

$ sed '/book/!d' file.txt 
line1: This is my book, a nice book.
line2: That is your book, a nice book.

示例:删除空白行,包括空格

$ sed  '/^\s*$/d' file.txt
line1: This is my book, a nice book.
line2: That is your book, a nice book.
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!

行替换

示例:把2 ~ 4行替换成delete:

$ sed '2,4 cdelete' file.txt 
line1: This is my book, a nice book.
delete
line5: Welcome to www.linuxbar.net!

内容搜寻

示例:查找哪些行包含字符串old:

$ sed -n '/old/=' file.txt 
3
4

示例:统计文件中一共有多少行:

$ sed -n '$=' file.txt 
5

级联操作

示例:把2 ~ 4行内容中的book替换成picture30替换成100

$ sed -n '2,4p' file.txt | sed 's/book/picture/g' | sed 's/30/100/g'
line2: That is your picture, a nice picture.
line3: I am 100 years old.
line4: You are 100 years old.

示例:把2 ~ 4行内容中的book替换成picture30替换成100

$ sed -n '2,4p' file.txt | sed  -e 's/book/picture/g' -e 's/30/100/g'
line2: That is your picture, a nice picture.
line3: I am 100 years old.
line4: You are 100 years old.

------ End ------


浏览 5
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报