Linux中的 SED 命令

29 篇文章 0 订阅

Linux中的 SED 命令

SED用于查找、过滤、文本替换、替换和文本操作,如插入、删除搜索等。它是Linux/Unix系统提供的功能强大的实用程序之一。我们可以将sed与正则表达式一起使用。


替换文件中 带有文件夹名(包含斜杠和双引号)的 字段

文件夹名:/fold_name_old
\:转义字符 $1:调用函数时 传的第一个参数,$2:第二个参数
/src" ➡️ /src_so_string2". ":双引号也需要转义。$2=string2

###参数定义
string1="aaa"
string2="bbb"

###替换函数
function sed_string_replace() {
	sed "s/\/fold_name_old/\/fold_name_new$1/g" file_old > file_new_out
	sed "s/\/src\"/\/src_so_$2\"/g" file_old > file_new_out
}

#替换方法调用:参数1 参数2
sed_string_replace $string1 $string2


它提供了文本文件的非交互式编辑,这就是为什么它被用来自动化编辑,并且有两个缓冲区——模式缓冲区和保持缓冲区。Sed在读取文件时使用Pattern buffer,逐行读取,并将当前读取的行插入到Pattern buffer中,而hold buffer是一个长期存储,它捕获信息,存储它,并在需要时重用它。最初,两者都是空的。SED命令可以在不打开文件的情况下执行不同的操作。

sed general syntax –
sed OPTIONS… [SCRIPT] [INPUTFILE…]

首先创建a.txt文件,我将对它执行SED命令的操作。在这个博客中,我使用了“a.txt”文件来解释所有的例子。如果我写每个sed命令的输出,博客将变得太长。因此,您可以参考同一个文件来练习所有的命令。

[root@rhel7 ~]# cat a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.

让我们从文件间距开始

1 -在每一行“-”后插入一个空行

[root@localhost linux_script]# sed G a.txt
life isn't meant to be easy, life is meant to be lived.

Try to learn & understand something new everyday in life.

Respect everyone & most important love everyone.

Don’t hesitate to ask for love & don’t hesitate to show love too.

Life is too short to be shy.

In life, experience will help you differentiating right from wrong.

[root@localhost linux_script]#

2 -插入两个空行-

[root@localhost linux_script]# sed 'G;G' a.txt
life isn't meant to be easy, life is meant to be lived.


Try to learn & understand something new everyday in life.


Respect everyone & most important love everyone.


Don’t hesitate to ask for love & don’t hesitate to show love too.


Life is too short to be shy.


In life, experience will help you differentiating right from wrong.


[root@localhost linux_script]#

3 -删除空行,并在每个“-”后插入一个空行

[root@localhost linux_script]# sed '/^$/d;G' a.txt
life isn't meant to be easy, life is meant to be lived.

Try to learn & understand something new everyday in life.

Respect everyone & most important love everyone.

Don’t hesitate to ask for love & don’t hesitate to show love too.

Life is too short to be shy.

In life, experience will help you differentiating right from wrong.

[root@localhost linux_script]#

4 – 在匹配到 “love” 的每一行上面 插入 空行 –

[root@localhost linux_script]# sed '/love/{x;p;x;}' a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.

Respect everyone & most important love everyone.

Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

5 -在匹配 “love” 的每一行下面插入空行-

[root@localhost linux_script]# sed '/love/G' a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.

Don’t hesitate to ask for love & don’t hesitate to show love too.

Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

6 在每一行的左边插入5个空格

[root@localhost linux_script]# sed 's/^/     /' a.txt
     life isn't meant to be easy, life is meant to be lived.
     Try to learn & understand something new everyday in life.
     Respect everyone & most important love everyone.
     Don’t hesitate to ask for love & don’t hesitate to show love too.
     Life is too short to be shy.
     In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

编号行

1 给文件的每一行编号(左对齐)。**=**用于给行编号。\t用于数字和句子之间的制表符

[root@localhost linux_script]# sed =  a.txt | sed 'N;s/\n/\t/'
1       life isn't meant to be easy, life is meant to be lived.
2       Try to learn & understand something new everyday in life.
3       Respect everyone & most important love everyone.
4       Don’t hesitate to ask for love & don’t hesitate to show love too.
5       Life is too short to be shy.
6       In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

2 -为文件的每一行编号(编号在左,右对齐)。该命令类似于’ cat -n filename '。

[root@localhost linux_script]# sed = a.txt | sed 'N; s/^/     /; s/ *\(.\{4,\}\)\n/\1  /'
   1  life isn't meant to be easy, life is meant to be lived.
   2  Try to learn & understand something new everyday in life.
   3  Respect everyone & most important love everyone.
   4  Don’t hesitate to ask for love & don’t hesitate to show love too.
   5  Life is too short to be shy.
   6  In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

3 -编号文件的每一行,只有当行不是空-

[root@localhost linux_script]# sed '/./=' a.txt | sed '/./N; s/\n/ /'
1 life isn't meant to be easy, life is meant to be lived.
2 Try to learn & understand something new everyday in life.
3 Respect everyone & most important love everyone.
4 Don’t hesitate to ask for love & don’t hesitate to show love too.
5 Life is too short to be shy.
6 In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

删除行

1 – 删除特定的一行 –
Syntax: sed ‘nd’ filename
例子 :

[root@localhost linux_script]# sed '5d' a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

2 -删除最后一行
Syntax: sed ‘$d’ filename

[root@localhost linux_script]# sed '$d' a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
[root@localhost linux_script]#

3 -删除范围x到y的行
Syntax: sed ‘x,yd’ filename
Example :

[root@localhost linux_script]# sed '3,5d' a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

4 -删除第n行到最后一行
Syntax: sed ‘nth,$d’ filename
Example :

[root@localhost linux_script]# sed '2,$d' a.txt
life isn't meant to be easy, life is meant to be lived.
[root@localhost linux_script]#

5 -删除模式匹配行“-”
Syntax: sed ‘/pattern/d’ filename
Example :

[root@localhost linux_script]# sed '/life/d' a.txt
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
[root@localhost linux_script]#

6 -删除从第n行开始的行和从那里开始的每2行-
Syntax: sed ‘n~2d’ filename
Example :

[root@localhost linux_script]# sed '3~2d' a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Don’t hesitate to ask for love & don’t hesitate to show love too.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

7 -删除匹配模式的行和后面的2行-
Syntax: sed ‘/pattern/,+2d’ filename
Example :

[root@localhost linux_script]# sed '/easy/,+2d' a.txt
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

8 -删除空行

[root@localhost linux_script]# sed '/^$/d' a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

9 -删除空行或以“#”-开头的行

[root@localhost linux_script]# sed -i '/^#/d;/^$/d' a.txt
[root@localhost linux_script]# cat a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

查看/打印文件

如果我们想查看文件的内容,那么我们使用cat命令,如果我们想查看任何文件的底部和顶部的内容,我们使用工具,如头和尾。但是,如果我们需要查看任何文件中间的特定部分,该怎么办呢?在这里,我们将讨论如何使用SED命令查看任何文件的某个部分。

1 -查看范围为x ~ y的文件
Syntax: sed -n ‘x,yp’ filename
Example :

[root@localhost linux_script]# sed -n '2,5p' a.txt
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
[root@localhost linux_script]#

2 –查看除给定范围外的整个文件 –
Syntax: sed ‘x,yd’ filename
Example :

[root@localhost linux_script]# sed '2,4d' a.txt
life isn't meant to be easy, life is meant to be lived.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

3 – 打印文件的第n行–
Syntax: sed -n ‘address’p filename
Example :

[root@localhost linux_script]# sed -n '4'p a.txt
Don’t hesitate to ask for love & don’t hesitate to show love too.
[root@localhost linux_script]#

4 -从第x行打印到第y行。
Syntax: sed -n ‘x,y’p filename
Example :

[root@localhost linux_script]# sed -n '4,6'p a.txt
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

5 -只打印最后一行-
Syntax: sed -n ‘$’p filename

[root@localhost linux_script]# sed -n '$'p a.txt
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

从第n行打印到文件末尾
Syntax: sed -n ‘n,$p’ filename
Example :

[root@localhost linux_script]# sed -n '3,$'p a.txt
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

7 -只打印匹配模式的行
Syntax: sed -n /pattern/p filename
Example :

[root@localhost linux_script]# sed -n /every/p a.txt
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
[root@localhost linux_script]#

打印匹配模式的行,例如从输入到第x行。
Syntax: sed -n ‘/pattern/,xp’ filename
Example :

[root@localhost linux_script]# sed -n '/everyone/,5p' a.txt
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
[root@localhost linux_script]#

下面打印匹配图案的行,第三行匹配图案“everyone”,所以从第三行打印到第五行。如果想打印文件到最后,使用$代替5。

打印从输入的第x行到匹配模式的行。如果没有找到模式,则打印到文件的末尾。
Syntax: sed -n ‘x,/pattern/p’ filename
Example :

[root@localhost linux_script]# sed -n '1,/everyone/p' a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
[root@localhost linux_script]#

10 -打印匹配模式的行直到下一个x行-
Syntax: sed -n ‘/pattern/,+xp’ filename
Example :

[root@localhost linux_script]# sed -n '/learn/,+2p' a.txt
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
[root@localhost linux_script]#

用sed命令替换

1 -改变第一次出现的模式-

[root@localhost linux_script]# sed 's/life/leaves/' a.txt
leaves isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in leaves.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In leaves, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

2 -替换在一行中出现的第n个图案-
Syntax: sed ‘s/old_pattern/new_pattern/n’ filename
Example :

[root@localhost linux_script]# sed 's/to/two/2' a.txt
life isn't meant to be easy, life is meant two be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate two show love too.
Life is too short two be shy.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

我们写" 2 "是因为我们替换了第二次出现。同样,你也可以根据需要使用3、4等。

3 -替换所有出现的模式在一行。

[root@localhost linux_script]# sed 's/life/learn/g' a.txt
learn isn't meant to be easy, learn is meant to be lived.
Try to learn & understand something new everyday in learn.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In learn, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

4 -替换模式从第n次出现到所有出现在一行。
Syntax: sed ‘s/old_pattern/new_pattern/ng’ filename
Example :

[root@localhost linux_script]# sed 's/to/TWO/2g' a.txt
life isn't meant to be easy, life is meant TWO be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate TWO show love TWOo.
Life is too short TWO be shy.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

注意:sed命令用“TWO”替换一行中第二个、第三个等出现的模式“to”。

如果您希望只打印被替换的行,那么使用“-n”选项和“/p”打印标志只显示被替换的行-

[root@localhost linux_script]# sed -n 's/to/TWO/p' a.txt
life isn't meant TWO be easy, life is meant to be lived.
Try TWO learn & understand something new everyday in life.
Don’t hesitate TWO ask for love & don’t hesitate to show love too.
Life is TWOo short to be shy.
[root@localhost linux_script]#

如果您希望打印替换后的行两次,则只使用“/p”打印标志,不使用“-n”选项-

[root@localhost linux_script]# sed 's/to/TWO/p' a.txt
life isn't meant TWO be easy, life is meant to be lived.
life isn't meant TWO be easy, life is meant to be lived.
Try TWO learn & understand something new everyday in life.
Try TWO learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate TWO ask for love & don’t hesitate to show love too.
Don’t hesitate TWO ask for love & don’t hesitate to show love too.
Life is TWOo short to be shy.
Life is TWOo short to be shy.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

5 -替换图案在一个特定的行号。这里,“m”是行号。
Syntax: sed ‘m s/old_pattern/new_pattern/’ filename
Example :

[root@localhost linux_script]# sed '3 s/every/each/' a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect eachone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

如果您希望只打印替换后的行-

[root@localhost linux_script]# sed -n '3 s/every/each/p' a.txt
Respect eachone & most important love everyone.
[root@localhost linux_script]#

6 -替换字符串的定义范围内的行-
Syntax: sed ‘x,y s/old_pattern/new_pattern/’ filename
where,
x = starting line number
and y = ending line number

Example :

[root@localhost linux_script]# sed '2,5 s/to/TWO/' a.txt
life isn't meant to be easy, life is meant to be lived.
Try TWO learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate TWO ask for love & don’t hesitate to show love too.
Life is TWOo short to be shy.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

注意:如果我们希望将模式更改到文件的最后一行,可以使用$来代替“y”。
Example :

[root@localhost linux_script]# sed '2,$ s/to/TWO/' a.txt
life isn't meant to be easy, life is meant to be lived.
Try TWO learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate TWO ask for love & don’t hesitate to show love too.
Life is TWOo short to be shy.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

7 -如果你想替换模式以忽略字符大小写(以大写或小写开头),那么有两种方法替换这样的模式-
First, By using “/i” print flag –
Syntax: sed ‘s/old_pattern/new_pattern/i’ filename
Example :

[root@localhost linux_script]#  sed 's/life/Love/i' a.txt
Love isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in Love.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Love is too short to be shy.
In Love, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

第二,通过使用正则表达式-

[root@localhost linux_script]# sed 's/[Ll]ife/Love/g' a.txt
Love isn't meant to be easy, Love is meant to be lived.
Try to learn & understand something new everyday in Love.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Love is too short to be shy.
In Love, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

8 -将多个空格替换为单个空格-

[root@localhost linux_script]# sed 's/  */ /g' a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

9 -替换一个模式后,再替换另一个模式-
Syntax: sed ‘/followed_pattern/ s/old_pattern/new_pattern/’ filename
Example :

[root@localhost linux_script]# sed '/is/ s/live/love/' a.txt
love isn't meant to be easy, life is meant to be loved.
Try to learn & understand something new everyday in love.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In love, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

10 -用另一个新模式替换现有的旧模式,除了第n行。
Syntax: sed ‘n!s/old_pattern/new_pattern/’ filename
Example :

[root@localhost linux_script]# sed -i '5!s/life/love/' a.txt
[root@localhost linux_script]# cat a.txt
love isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in love.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In love, experience will help you differentiating right from wrong.
[root@localhost linux_script]#

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小泉映月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值