-n 使用安静(silent)模式,只列出经过sed特殊处理的那一行(或者动作)内容;
-e 直接在指令列模式上进行 sed 的动作编辑;
-f 直接将 sed 的动作写在一个文件内, -f filename 则可以执行filename内的sed命令;
-r 让sed命令支持扩展的正则表达式(默认是基础正则表达式);
-i 直接修改读取的文件内容,而不是由屏幕输出;
常用的命令有以下几种:
a \: 即append追加字符串,可将其后的字符加在所选择内容的后面
c \: 取代/替换字符串,可将其后内容替换至所选内容
d : 即delete删除,该命令会将当前选中的行删除
i \: 即insert插入字符串,可将其后内容插入至所选内容前
p : print即打印,该命令会打印当前选择的行到屏幕上
s : 替换,通常s命令的用法是这样的:1,2s/old/new/g,将old字符串替换成new字符串
命令示例
假设有一个本地文件test.txt,文件内容如下:
[root@linuxprobe ~]$ cat test.txt
this is first line
this is second line
this is third line
this is fourth line
this fifth line
happy everyday
end
本节将使用该文件详细演示每一个命令的用法。
a命令
[root@linuxprobe ~]$ sed '1a \add one' test.txt
this is first line
add one
this is secondline
this is third line
this is fourth line
this is fifth line
happy everyday
end
本例命令部分中的1表示第一行,同样的第二行写成2,第一行到第三行写成1,3,用
表示最后一行,比如
2
,
表示最后一行,比如2,
表示最后一行,比如2,表示第二行到最后一行中间所有的行(包含第二行和最后一行)。
本例的作用是在第一行之后增加字符串”add one”,从输出可以看到具体效果。
[root@linuxprobe ~]$ sed '1,$a \add one' test.txt
this is first line
add one
this is second line
add one
this is third line
add one
this is fourth line
add one
this is fifth line
add one
happy everyday
add one
end
add one
本例表示在第一行和最后一行所有的行后面都加上”add one”字符串,从输出可以看到效果。
[root@linuxprobe ~]$ sed '/first/a \add one' test.txt
this is first line
add one
this is secondline
this is third line
this is fourth line
this is fifth line
happy everyday
end
本例表示在包含”first”字符串的行的后面加上字符串”add one”,从输出可以看到第一行包含first,所以第一行之后增加了”add one”
[root@linuxprobe ~]$ sed '/^ha.*day$/a \add one' test.txt
this is first line
this is secondline
this is third line
this is fourth line
this is fifth line
happy everyday
add one
end
本例使用正则表达式匹配行,^ha.*day$表示以ha开头,以day结尾的行,则可以匹配到文件的”happy everyday”这样,所以在该行后面增加了”add one”字符串。
i命令
i命令使用方法和a命令一样的,只不过是在匹配的行的前面插入字符串,所以直接将上面a命令的示例的a替换成i即可,在此就不啰嗦了。
c命令
[root@linuxprobe ~]$ sed '$c \add one' test.txt
this is first line
this is secondline
this is third line
this is fourth line
this is fifth line
happy everyday
add one
本例表示将最后一行替换成字符串”add one”,从输出可以看到效果。
[root@linuxprobe ~]$ sed '4,$c \add one' test.txt
this is first line
this is secondline
this is third line
add one
本例将第四行到最后一行的内容替换成字符串”add one”。
[root@linuxprobe ~]$ sed '/^ha.*day$/c \replace line' test.txt
this is first line
this is secondline
this is third line
this is fourth line
this is fifth line
replace line
end
本例将以ha开头,以day结尾的行替换成”replace line”。
d命令
[root@linuxprobe ~]$ sed '/^ha.*day$/d' test.txt
this isfirst line
this issecond line
this isthird line
this isfourth line
this isfifth line
end
本例删除以ha开头,以day结尾的行。
[root@linuxprobe ~]$ sed '4,$d' test.txt
thisis first line
thisis second line
thisis third line
最后的话
最近很多小伙伴找我要Linux学习资料,于是我翻箱倒柜,整理了一些优质资源,涵盖视频、电子书、PPT等共享给大家!
资料预览
给大家整理的视频资料:
给大家整理的电子书资料:
如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!