sed——Shell 文本处理利器

一、简介

sed全称是stream editor,是一个用于文本过滤和替换的流编辑器,它是一个管道命令,数据源来自stdin,它的最小处理单位是行(与awk区分)。

二、语法

语法如下:

sed [-nfri] [动作]
-n:使用安静模式。只有经过sed处理的行才会输出到屏幕上
-f:直接将sed的动作写在一个文件内,-f filename 则可以执行filename内的sed动作
-r:切换为支持扩展正则表达式
-i:直接修改读取的文件内容,而不是由屏幕输出

[动作]
[n1[,n2]]function
n1 n2表示应用sed的行数,可以不存在该参数

function参数说明:
a:新增,a的后面可以接字符串,这些字符串会出现在下一行(新行)
c:替换,c的后面可以接字符串,这些字符串将替换n1到n2之间的行
d:删除,删除n1到n2之前的行
i:插入,i的后面可以接字符串,这些字符串会出现在上一行(新行)
p:打印,也就是将某个选择的数据打印出来。通常p会与sed -n一起使用
s:替换,如将1到20行之间的old替换为new:1,20s/old/new/g

三、测试文本

测试文本的内容如下所示,创建一个新文件regular_express.txt,并将下面的内容拷贝到文件中即可开始试验:

Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ 3183 dollars.^M
GNU is free air not free beer.^M
Her hair is very beauty.^M
I can't finish the test.^M
Oh! The soup taste good.^M
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh!     My god!
The gd software is a library for drafting programs.^M
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.
# I am VBird

四、删除实例d

使用sed删除文件的2,5行:

[nigel]$ nl regular_express.txt | sed '2,5d'
     1  "Open Source" is a good mechanism to develop programs.
     6  GNU is free air not free beer.^M
     7  Her hair is very beauty.^M
     8  I can't finish the test.^M

如果要删除第3行到最后一行,则可以这样:

[nigel@/vbird_linux]$ nl regular_express.txt | sed '3,$d'
     1  "Open Source" is a good mechanism to develop programs.
     2  apple is my favorite food.

只删除第2行:

[nigel@/vbird_linux]$ nl regular_express.txt | sed '2d'
     1  "Open Source" is a good mechanism to develop programs.
     3  Football game is not use feet only.

五、插入实例a,i

[nigel@/vbird_linux]$ nl regular_express.txt | sed '2a Hello world'
     1  "Open Source" is a good mechanism to develop programs.
     2  apple is my favorite food.
Hello world
     3  Football game is not use feet only.

[nigel@/vbird_linux]$ nl regular_express.txt | sed '2i Hello world'
     1  "Open Source" is a good mechanism to develop programs.
Hello world
     2  apple is my favorite food.
     3  Football game is not use feet only.

多行的插入比较特殊:

[nigel@/vbird_linux]$ nl regular_express.txt | sed '2i Hello \
> world!'
     1  "Open Source" is a good mechanism to develop programs.
Hello
world!
     2  apple is my favorite food.
     3  Football game is not use feet only.

多行插入必须要加上\。

六、整行替换c

动作c可以将n1-n2行替换为指定的字符串:

[nigel/vbird_linux]$ nl regular_express.txt | sed '2,5c Hello world'
     1  "Open Source" is a good mechanism to develop programs.
Hello world
     6  GNU is free air not free beer.^M

注意,是将n1-n2替换为一行,总行数会减少。

七、打印p

我们以前要列出11-20行需要这么做“head -n 20 | tail -n 10”,这样处理很麻烦。有了sed后,就可以简单直接取出那几行:

[nigel@ /vbird_linux]$ nl regular_express.txt | sed -n '11,15p'
    11  This window is clear.
    12  the symbol '*' is represented as start.
    13  Oh!     My god!
    14  The gd software is a library for drafting programs.^M
    15  You are the best is mean you are the no. 1.

注意要与-n参数一起使用。

八、部分数据的查找并替换s

用法

sed 's/旧字符串/新字符串/g'

使用示例:

[nigel@/vbird_linux]$ nl regular_express.txt | sed  's/apple/APPLE/g'
     1  "Open Source" is a good mechanism to develop programs.
     2  APPLE is my favorite food.

九、直接修改文件

sed除了能从管道后去数据外,还能够直接修改文件!

[nigel@/vbird_linux]$ cat regular_express.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
[nigel@/vbird_linux]$ sed -i '1,2d' regular_express.txt
[nigel@/vbird_linux]$ cat regular_express.txt
Football game is not use feet only.

不过该功能需要慎用,一旦修改过后就不能恢复。

PS:本文章内容学习自《鸟哥的linux私房菜 基础学习篇(第三版)》,文章中的所有实例都是亲手操作,保证有效。

猜你喜欢

转载自blog.csdn.net/zhoucheng05_13/article/details/82657668