Linux sed命令的用法介绍

本文详细介绍了SED命令的基础知识、常用选项及脚本命令。通过实际案例演示了如何利用SED进行文本处理,包括行的增删查改等操作。

1. sed命令简介

sed是非交互式的编辑器。它不会修改文件,除非使用shell重定向来保存结果。默认情况下,所有的输出行都被打印到屏幕上。

sed编辑器逐行处理文件(或输入),并将结果发送到屏幕。具体过程如下:首先sed把当前正在处理的行保存在一个临时缓存区中(也称为模式空间),然后处理临时缓冲区中的行,完成后把该行发送到屏幕上。sed每处理完一行就将其从临时缓冲区删除,然后将下一行读入,进行处理和显示。处理完输入文件的最后一行后,sed便结束运行。sed把每一行都存在临时缓冲区中,对这个副本进行编辑,所以不会修改原文件。

2. sed命令格式

sed命令的一般格式为:sed [选项] “脚本” 文件

(下文中“命令”都是指sed的命令部分。)

选项:

sed的选项形如-n, -e, -f等,都有个'-'。

-n    忽略默认输出(默认输出就是整个文件的内容)。

-e    执行多个编辑任务

-f     执行文件中的命令集合

脚本:

sed的脚本有p, d, i\等,文中会重点介绍。

如果脚本部分为空,则可以用来打印一个文件的内容:

[root@ubuntu]sed_test:$ sed "" sed_test.txt

  1 It's been a long day without you my friend

  2 And I'll tell you all about it when I seeyou again

  3 We've come a long way from where we began

  4 Oh I'll tell you all about it when I seeyou again

  5 When I see you again

sed支持正则表达式:

元字符

 功能

 ^

 行首定位符

 $

 行尾定位符

 .

 匹配除换行符以外的单个字符

 *

 匹配零个或多个前导字符

\?

匹配零个或1个前导字符

\+

匹配1个或多个前导字符

\|

或操作

\s

匹配单个空白字符(如\t

\S

匹配单个非空白字符

\w

匹配单个单词

\W

匹配单个非单词

 []

 匹配指定字符组内的任一字符

 [^]

 匹配不在指定字符组内的任一字符

[-]

匹配字符集的字符范围

 (..)

 保存已匹配的字符(括号前面要加反斜杠\,csdn没显示出来)

 &

 保存查找串以便在替换串中引用

 \<

 词首定位符

 \>

 词尾定位符

 x\{m\}

 连续mx

 x\{m,\}

 至少连续mx

 x\{m,n\}

 至少连续m个,但不超过nx

(str)

正则表达式中对字符串的引用(括号前面要加反斜杠\,csdn没显示出来)

中括号表达式

[:alnum:][:alpha:],[:digit:][:lower:]

[:upper:][:punct:][:space:]

3. sed选项和命令介绍

选项和(脚本中的)命令我会穿插在一起介绍,大家注意区分带“-”的是选项,否则是命令。

p     打印行

例:上面的sed_test.txt文件为例。

打印第3行的内容:

[root@ubuntu]sed_test:$sed -n "3p" sed_test.txt

  3 We've come a long way from where we began

把3和p分开也可以,并且更能分辨出p是个命令。

注意加-n选项是为了不显示默认输出,如果这里不加-n,效果是这样的:

[root@ubuntu]sed_test:$sed "3p" sed_test.txt

  1 It's been a long day without you my friend

  2 And I'll tell you all about it when I see you again

 3 We've come a longway from where we began

  3 We've come a longway from where we began

  4 Oh I'll tell you all about it when I see you again

  5 When I see you again

即既打印出所有内容,又打印出了红色部分的内容。由红色部分的位置可知,sed是进行行操作的。

如下,addr1,addr2addr1,+Naddr1~stepaddr1,~N可以表示寻址范围。

打印1-3行的内容(注意行数是从1开始的):

[root@ubuntu]sed_test:$sed -n "1,3p" sed_test.txt

  1 It's been a long day without you my friend

  2 And I'll tell you all about it when I see you again

  3 We've come a long way from where we began


打印第三行及后续2行:

[root@ubuntu]sed_test:$sed -n "3,+2p" sed_test.txt

  3 We've come a long way from where we began

  4 Oh I'll tell you all about it when I see you again

  5 When I see you again


打印第2行以及4-10行:

[root@ubuntu]sed_test:$sed -n "2p;4,10p" sed_test.txt

  2 And I'll tell you all about it when I see you again

  4 Oh I'll tell you all about it when I see you again

  5 When I see you again

 

“first~step”形式表示从first行作为0计数起点,打印step倍数行的内容。

打印第3行开始2的倍数行,即3,5,7 ...行的内容:

[root@ubuntu]sed_test:$sed -n "3~2p" sed_test.txt

那么打印所有奇数行:

[root@ubuntu]sed_test:$sed -n "1~2p" sed_test.txt

 

“addr1,~N”形式表示从addr1行开始,到找到N的倍数行结束。

从第13行开始,到5的倍数行结束,这里即第15行结束:

[root@ubuntu]sed_test:$sed -n "13,~5p" sed_test.txt

/regexp/、\%regexp% 匹配行

用来寻找匹配正则表达式regexp的行。

例如,打印包含“again”的行:

[root@ubuntu]sed_test:$ sed -n "/again/p" see_you_again.lrc

And I'll tell you all about it when I see you again

Oh I'll tell you all about it when I see you again

When I see you again

如果需要匹配斜杠“/”,则需要用反斜杠转义“\/”。

\%regexp%的形式仍然是匹配行,其中%可以替换为任意字符。这种形式可以用在regexp里包含斜杠“/”的情况,这样就不需要用反斜杠转义。

/regexp/I、\%regexp%I  匹配行(忽略大小写)

后面添加大写的I,表示匹配时忽略大小写。

d     删除行

删除2-4行的内容:

[root@ubuntu]sed_test:$sed "2,4d" sed_test.txt

  1 It's been a long day without you my friend

  5 When I see you again

注意-n的作用就体现出来了,这里不能加-n。注意,这并没有修改文件本身的内容。

 

移除文件中的空行:

[root@ubuntu]sed_test:$  sed "/^$/d" sed_test.txt

 

删除包含“We”和“When”的行以及他们之间的行:

[root@ubuntu]sed_test:$sed "/We/,/When/d" see_you_again.lrc

It's been a long day without you my friend

And I'll tell you all about it when I see you again

正则表达式

举例:see_you_again.lrc的内容如下:

[root@ubuntu]sed_test:$sed '' see_you_again.lrc

It's been a long day without you my friend

And I'll tell you all about it when I see you again

We'vecome a long way from where we began

Oh I'll tell you all about it when I see you again

When I see you again

 

打印包含“again”的行:

[root@ubuntu]sed_test:$sed -n "/again/p" see_you_again.lrc

And I'll tell you all about it when I see you again

Oh I'll tell you all about it when I see you again

When I see you again

 

打印不以“We”开头的行:

[root@ubuntu]sed_test:$sed -n "/^We/! p" see_you_again.lrc

It'sbeen a long day without you my friend

And I'll tell you all about it when I see you again

Oh I'll tell you all about it when I see you again

When I see you again

 

在kallsyms文件中查找包含“[gmac]”的行,并在这些行中找到不以83aa开头的行:

 

打印以“again”结尾的行:

[root@ubuntu]sed_test:$sed -n "/again$/p" see_you_again.lrc

And I'll tell you all about it when I see you again

Oh I'll tell you all about it when I see you again

When I see you again

 

打印包含“I” “you”,中间为任意多个字符的行:

[root@ubuntu]sed_test:$sed -n "/I.*you/p" see_you_again.lrc

It's been a long day without you my friend

And I'll tell you all about it when I see you again

Oh I'll tell you all about it when I see you again

When I see you again

 

打印包含“I”,但后面不跟字母的行:

[root@ubuntu]sed_test:$sed -n "/I[^a-zA-Z]/p" see_you_again.lrc

And I'll tell you all about it when I see you again

Oh I'll tell you all about it when I see you again

When I see you again

 

打印包含2个连续的“e”字母的行:

[root@ubuntu]sed_test:$sed -n "/e\{2\}/p" see_you_again.lrc

It's been a long day without you my friend

And I'll tell you all about it when I see you again

Oh I'll tell you all about it when I see you again

When I see you again

 

注意,上面都是针对单个字符的匹配,如果要匹配一个字符串,要加小括号:

打印包含1到2个连续“bang”的行:

另外需要注意的是,中括号[]里都是匹配单个字符的,无法匹配一个字符串。

 

打印包含以“wh”开头的单词的行:

[root@ubuntu]sed_test:$sed -n "/\<wh/p" see_you_again.lrc

And I'll tell you all about it when I see you again

We've come a long way from where we began

Oh I'll tell you all about it when I see you again

s     用一个字符串替换另一个字符串

g     在行内进行全局替换

i      替换时不区分大小写

(…)      对字符串分组

将包含“you”的行中的“you”替换为“her”:

[root@ubuntu]sed_test:$sed -n "s/you/her/gp" see_you_again.lrc

It's been a long day without her my friend

And I'll tell her all about it when I see her again

Oh I'll tell her all about it when I see her again

When I see her again

 

将包含“you”的行中的“you”替换为“her”,只替换每行的第一处:

[root@ubuntu]sed_test:$sed -n "s/you/her/p" see_you_again.lrc

It's been a long day without her my friend

And I'll tell her all about it when I see you again

Oh I'll tell her all about it when I see you again

When I see her again

 

将包含“long”的行中的“long”替换为“tough”,不区分大小写:

[root@ubuntu]sed_test:$sed -n "s/LOng/tough/pi" see_you_again.lrc

It's been a tough day without you my friend

We've come a tough way from where we began

 

可以用(…)对字符串进行分组,并且在后面使用1,2,3…依次代替匹配列表,例如,将“long”和“way”中间加一个“-”:

 

将4-5行的“you”替换为“her”:

[root@ubuntu]sed_test:$sed -n "4,5s/you/her/gp" see_you_again.lrc

Oh I'll tell her all about it when I see her again

When I see her again

 

将包含“long”的行中的空格替换为逗号:

[root@ubuntu]sed_test:$sed -n "/long/s/ /,/gp" see_you_again.lrc

It's,been,a,long,day,without,you,my,friend

We've,come,a,long,way,from,where,we,began

 

匹配包含“friend”以及“Oh”的行,并同时打印出他们之间的所有行(应用所有匹配行):

[root@ubuntu]sed_test:$sed -n "/friend/,/Oh/ p" see_you_again.lrc

It's been a long day without you my friend

And I'll tell you all about it when I see you again

We've come a long way from where we began

Oh I'll tell you all about it when I see you again

如果逗号前面的模式没有匹配行,则从第1行开始打印;如果逗号后面的模式没有匹配行,则打印到最后一行。

 

打印包含“friend”的行,并同时打印所有匹配行的后续2行:

[root@ubuntu]sed_test:$sed -n "/friend/,+2 p" see_you_again.lrc

It's been a long day without you my friend

And I'll tell you all about it when I see you again

We've come a long way from where we began

可见“,”号和“+”号和前面讲p命令时有着相同的意义。

 

分隔符默认为正斜杠,但可以改变。无论什么字符(换行符除外),只要紧跟s命令,就成了新的串分隔符。如下面的字符z:

[root@ubuntu]sed_test:$sed -n "4,5szyouzherzgp" see_you_again.lrc

Oh I'll tell her all about it when I see her again

When I see her again

 

将Makefile文件中前3行注释掉:

[root@ubuntu]sed_test:$  sed '1,3s/^/#/' Makefile

 

将空格改为“,0x”,然后将每行最后两个字符删掉,然后将每行开头加上“0x”:

sed -n "s/ /,0x/g;s/..$//g;s/^/0x/p" spcontent.txt

 

将每行的第三个空格改为逗号:

sed -n "s/ /,/3p" spcontent.txt

 

将每行的第三个和第四个空格改为逗号(注意第一次改完之后第四个空格对于第二次来讲是第三个空格):

sed -n "s/ /,/3;s/ /,/3p" spcontent.txt

 

将每行的第三个以及之后所有的空格改为逗号:

sed -n "s/ /,/3gp" spcontent.txt

 

另外,我们知道vim中进行字符串替换的命令也是s和g,例如,将文件中的nihao改为hello,命令为“:s/nihao/hello/g”,而“:m,ns/nihao/hello/g”为将m-n行的nihao改为hello。和sed中的替换命令类似。

-e    用于执行多个编辑任务

将“you”变成“her”,并且将“again”删掉:

[root@ubuntu]sed_test:$sed -n -e "s/you/her/g" -e "s/again//gp" see_you_again.lrc

And I'll tell her all about it when I see her

Oh I'll tell her all about it when I see her

When I see her

 

使用分号可以分隔两个命令,这样,两个命令可以写在一行,所以上面还可以这样写:

[root@ubuntu]sed_test:$sed -n "s/you/her/g;s/again//gp" see_you_again.lrc

And I'll tell her all about it when I see her

Oh I'll tell her all about it when I see her

When I see her

r,  R  读命令

在see_you_again.lrc中查找包含“long”的行,并将文件newone.txt的内容追加到该行后面:

[root@ubuntu]sed_test:$cat newone.txt

Damn,who knew all the planes we flew

[root@ubuntu]sed_test:$sed "/long/r newone.txt" see_you_again.lrc

It's been a long day without you my friend

Damn, who knew all the planes we flew

And I'll tell you all about it when I see you again

We've come a long way from where we began

Damn, who knew all the planes we flew

Oh I'll tell you all about it when I see you again

When I see you again

如果使用R命令,只在第一个匹配行后面追加newone.txt的内容。

w,  W  写命令

在see_you_again.lrc中查找包含“long”的行,并将该行内容写到newone.txt文件中:

[root@ubuntu]sed_test:$sed -n "/long/w newone.txt" see_you_again.lrc

可以看到newone.txt中的内容被清空并且写入了新的内容。

 

将see_you_again.lrc中的奇数行写到newone.txt文件中:

[root@ubuntu]sed_test:$  sed -n "1~2w newone.txt" see_you_again.lrc

大写W命令则只将模式空间中第一行的内容写到newone.txt文件中。

a \   追加新行

在包含“see you again”的行后面添加一行,内容为“your pretty face”:

[root@ubuntu]sed_test:$sed "/see you again/a \your pretty face" see_you_again.lrc

It's been a long day without you my friend

And I'll tell you all about it when I see you again

your pretty face

We've come a long way from where we began

Oh I'll tell you all about it when I see you again

your pretty face

When I see you again

your pretty face

该命令不加反斜杠也可以,即aa\a \都可以。

 

在第2行后面追加your pretty face:

[root@ubuntu]sed_test:$sed "2a \your pretty face" see_you_again.lrc

It's been a long daywithout you my friend

And I'll tell you all about it when I see you again

your pretty face

We've come a long way from where we began

Oh I'll tell you all about it when I see you again

When I see you again

 

将文件末尾插入your pretty face:

[root@ubuntu]sed_test:$sed "$ a \your pretty face" see_you_again.lrc

It'sbeen a long day without you my friend

And I'll tell you all about it when I see you again

We've come a long way from where we began

Oh I'll tell you all about it when I see you again

When I see you again

your pretty face

i \    插入新行

和a\命令的区别是,a\是在行的后面追加新行,而i\是在行的前面插入新行。

在文件开头插入your pretty face:

[root@ubuntu]sed_test:$sed "1i \your pretty face" see_you_again.lrc

your pretty face

It's been a long day without you my friend

And I'll tell you all about it when I see you again

We've come a long way from where we began

Oh I'll tell you all about it when I see you again

When I see you again

c \   替换行

将包含“see you again”的行内容替换为“your pretty face”:

[root@ubuntu]sed_test:$sed "/see you again/c\your pretty face" see_you_again.lrc

It's been a long day without you my friend

your pretty face

We've come a long way from where we began

your pretty face

your pretty face

y     字符替换

将文件中所有的y换成Y,o换成O,u换成U,空格换成逗号:

[root@ubuntu]sed_test:$sed "y/you /YOU,/" see_you_again.lrc

It's,been,a,lOng,daY,withOUt,YOU,mY,friend

And,I'll,tell,YOU,all,abOUt,it,when,I,see,YOU,again

We've,cOme,a,lOng,waY,frOm,where,we,began

Oh,I'll,tell,YOU,all,abOUt,it,when,I,see,YOU,again

When,I,see,YOU,again

正则表达式元字符对y命令不起作用。由于是一一对应的替换,所以y命令中替换和被替换的字母串中字母数目必须相同。

和s命令一样,斜杠可以换成其他字符。

 

将第8行中的“/”换成“\”:

[root@zhenfg sed_test]# sed "8y#/#\\\#" sed_test.txt

反斜杠是特殊字符,所以要处理一下子。又如,双引号也是特殊字符,所以要换转双引号也要加反斜杠转义:

将第8行的双引号改为单引号:

[root@zhenfg sed_test]# sed "8y#\"#'#" sed_test.txt

-f 执行文件中的命令集合

sed还支持以下格式,可以从scriptfile里读取命令部分:

sed [-n] -fscriptfile files

例如:

[root@ubuntu]sed_test:$cat commands.txt

s/you/her/

s/ /,/g

[root@ubuntu]sed_test:$  sed -f commands.txt see_you_again.lrc

It's,been,a,long,day,without,her,my,friend

And,I'll,tell,her,all,about,it,when,I,see,you,again

We've,come,a,long,way,from,where,we,began

Oh,I'll,tell,her,all,about,it,when,I,see,you,again

When,I,see,her,again

我们看到,可以把多条命令写到文件里一起执行,相当于-e选项。

l 显示隐藏字符

举例略。

q [exit-code]、Q [exit-code] 停止执行流

打印3行就停止:

[root@ubuntu]sed_test:$sed "3q" see_you_again.lrc

It's been a long day without you my friend

And I'll tell you all about it when I see you again

We've come a long way from where we began

 

匹配到“again”就停止:

[root@ubuntu]sed_test:$  sed "/again/q" see_you_again.lrc

It's been a long day without you my friend

And I'll tell you all about it when I see you again


q后面可以接数字,可作为sed命令的返回值:

[root@ubuntu]sed_test:$ sed "/again/q 14" see_you_again.lrc

It's been a long day without you my friend

And I'll tell you all about it when I see you again

[root@ubuntu]sed_test:$ echo $?

14

大写Q的效果如下,即匹配的那一行不会打印出来:

[root@ubuntu]sed_test:$ sed "/again/Q" see_you_again.lrc

It's been a long day without you my friend

e 执行外部命令

在第1行前面添加date命令的结果:

[root@ubuntu]sed_test:$  sed "1 e date" see_you_again.lrc

2017年 01月 01日 星期日 02:36:26 CST

It's been a long day without you my friend

And I'll tell you all about it when I see you again

We've come a long way from where we began

Oh I'll tell you all about it when I see you again

When I see you again

 

也可以直接执行文件中的命令:

[root@ubuntu]sed_test:$  sed 'e' shell.sh

n 单行next,移动到文本的下一行

找到包含“friend”的行,然后删除它的下一行:

[root@ubuntu]sed_test:$  sed "/friend/{n;d}" see_you_again.lrc

It's been a long day without you my friend

We've come a long way from where we began

Oh I'll tell you all about it when I see you again

When I see you again

= 输出行号

例如,显示文件的行号信息:

[root@ubuntu]sed_test:$  sed "=" see_you_again.lrc

1

It's been a long day without you my friend

2

And I'll tell you all about it when I see you again

3

We've come a long way from where we began

4

Oh I'll tell you all about it when I see you again

5

When I see you again


出包含“We”的行的行号:

[root@ubuntu]sed_test:$  sed -n "/We/=" see_you_again.lrc

3

 

输出文件的行数:

[root@ubuntu]sed_test:$  sed -n "$=" see_you_again.lrc

5

& 用于存储模式匹配的内容

&命令通常与s命令配合使用,例如,在每一行的末尾追加字符串“ end”:

[root@ubuntu]sed_test:$  sed "s/$/& end/g" see_you_again.lrc

It's been a long day without you my friend end

And I'll tell you all about it when I see you again end

We've come a long way from where we began end

Oh I'll tell you all about it when I see you again end

When I see you again end

这里&就指代前面的模式匹配到的内容。

-i[suffix]  修改文件

使用-i选项可以修改原文件,例如,删除script.sh中以#开头的行:

[root@ubuntu]sed_test:$ sed -i "/^#/d" script.sh

在-i后面加一个后缀,就会产生以“原文件名+suffix”命名的备份文件。

sed -i.bak "/^#/d" script.sh

#  注释

可以在sed的脚本部分中添加注释,注释的内容以一个新行结束,或者在一个-e片段的末尾结束。

4. 总结

上面讲到的一些命令在某些机器上可能不支持,大家可以通过man sed来查看支持的选项和命令。

Sed的使用还是要靠平时的积累,用的多了,自然就熟悉了。

本文只是介绍一些简单且常用的选项和脚本命令,详细的sed介绍可以参考GNU的sed manual:

http://www.gnu.org/software/sed/manual/sed.html


 

 

PS:严重吐槽CSDN的排版,经常少复制一个空格,排版有时会乱,并且这些字符会被转义,无法显示,所以我只能截图。吐槽完毕~

 

<think>嗯,用户想了解Linuxsed命令的详细用法和实例。首先,我需要回忆一下sed的基本功能和常见用途。sed是流编辑器,主要用于文本处理,比如替换、删除、插入等操作。用户可能已经知道一些基础,但需要更深入的指导,包括语法和例子。 接下来,我得组织回答的结构。通常,用户喜欢分步骤或分部分的内容,这样容易理解。可能分为简介、语法、常用命令、实例、高级技巧和常见问题这几个部分。不过用户提供的示例里引用了替换、删除、插入、行寻址等,可能这些是重点。 要注意用户提到的系统级指令,比如行内数学公式用$...$,但这里可能用不上。不过要确保中文回答,正确使用LaTeX格式,如果有数学表达式的话。但sed命令主要涉及文本处理,可能不需要数学公式,但如果有例子中的正则表达式,可能需要用代码块来展示。 用户还要求生成相关问题,我需要考虑用户可能进一步想知道的内容,比如sed和awk的区别,正则表达式的高级用法,或者如何调试sed脚本。这些可以作为相关问题放在最后。 现在检查用户提供的引用内容。引用[1]提到sed是强大的文本处理工具,结合正则表达式和多个命令。引用[2]提到高级用法,如分支命令和流程控制。所以,在回答中需要包括基础命令和高级用法,比如分支、循环,以及如何组合多个命令。 用户可能的需求包括实际例子,比如替换文件中的文本、删除特定行、插入内容等。需要给出具体的命令示例,并解释每个参数的作用。例如,替换命令s的用法,如何指定行范围,使用不同的分隔符等。 另外,用户可能对sed的工作流程不太清楚,比如读取行到模式空间,处理后再输出。需要简要说明sed的处理过程,这样用户能更好地理解命令的效果,尤其是在处理多行或保持空间时。 还要注意常见问题,比如原地编辑文件需要-i选项,处理特殊字符时的转义,以及如何同时执行多个命令。这些是用户在实际使用中可能会遇到的坑,需要提前说明。 在高级技巧部分,可以介绍正则表达式的分组、反向引用,以及使用标签控制流程。例如,如何跳过某些处理步骤,或者根据条件执行不同的命令。这些内容在引用[2]中提到了分支命令b,所以需要详细解释。 最后,生成的相关问题需要围绕用户可能进一步探索的方向,比如sed与其他工具的比较、复杂文本处理案例、调试技巧等。确保问题能够引导用户深入学习。 总结一下,回答的结构应该是:简介、语法结构、常用命令、实例演示、高级技巧、常见问题、相关问题。每个部分用简洁的语言和示例说明,确保用户能一步步掌握sed用法。同时,注意引用中的内容,适当融入高级用法的讲解,比如分支命令和流程控制,以增强回答的深度。</think>### Linux sed命令详解及实例教程 --- #### 一、sed命令简介 sed(Stream Editor)是Linux中基于文本流的非交互式编辑器,通过逐行处理输入流实现文本替换、删除、插入等操作,支持正则表达式[^1]。 --- #### 二、基本语法 ```bash sed [选项] '命令' 输入文件 ``` **常用选项**: - `-i`:直接修改文件内容(慎用) - `-n`:仅显示处理后的行 - `-e`:执行多条命令(如 `sed -e 's/a/A/' -e 's/b/B/'`) - `-r`:启用扩展正则表达式 --- #### 三、核心操作命令 1. **替换命令 `s`** ```bash sed 's/原内容/新内容/[修饰符]' file.txt ``` - **修饰符**: - `g`:全局替换(默认仅替换每行首个匹配- `p`:打印替换后的行 - `i`:忽略大小写 - **实例**: ```bash # 将文件中的"apple"替换为"orange"(仅首个匹配sed 's/apple/orange/' file.txt # 全局替换并保存文件 sed -i 's/apple/orange/g' file.txt ``` 2. **删除命令 `d`** ```bash # 删除第3行 sed '3d' file.txt # 删除包含"error"的行 sed '/error/d' file.txt ``` 3. **插入/追加命令 `i` 和 `a`** ```bash # 在第2行前插入内容 sed '2i\Insert this line' file.txt # 在匹配行后追加内容 sed '/pattern/a\Append this line' file.txt ``` 4. **行寻址** - **范围操作**:`起始行,结束行` ```bash # 删除1到5行 sed '1,5d' file.txt # 对包含"start"到"end"的行执行替换 sed '/start/,/end/s/old/new/g' file.txt ``` --- #### 四、高级用法 1. **正则表达式分组** ```bash # 交换第1列和第2列(使用分组和反向引用) sed 's/\([^ ]*\) \([^ ]*\)/\2 \1/' file.txt ``` 2. **多命令组合** ```bash # 删除空行并替换内容 sed -e '/^$/d' -e 's/foo/bar/g' file.txt ``` 3. **分支命令 `b`(流程控制)** ```bash # 若行包含"skip",跳过后续处理 sed '/skip/{b end;}; s/old/new/; :end' file.txt ``` --- #### 五、经典实例 1. **提取日志中的时间戳** ```bash sed -n 's/.*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\).*/\1/p' log.txt ``` 2. **批量重命名文件** ```bash ls *.txt | sed 's/\(.*\)\.txt/mv & \1.md/' | sh ``` 3. **处理多行文本** ```bash # 合并连续空行为单行 sed ':a; N; /\n$/ba; s/\n\+/\n/;' file.txt ``` --- #### 六、常见问题 1. **原地修改文件需谨慎**:使用 `-i` 前建议先备份或测试命令。 2. **特殊字符转义**:若替换内容含 `/`,可改用其他分隔符(如 `s#old#new#`)。 3. **性能优化**:处理大文件时避免频繁写操作,优先使用流式处理。 ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值