sed编辑器逐行处理输入,然后把结果发送到屏幕。
sed命令和选项:
a\ |
在当前行后添加一行或多行 |
c\ |
用新文本替换当前行中的文本 |
d |
删除行 |
i\ |
在当前行之前插入文本 |
h |
把模式空间的内容复制到暂存缓冲区 |
H |
把模式空间的内容添加到缓冲区 |
g |
取出暂存缓冲区的内容,将其复制到模式缓冲区 |
G |
取出暂存缓冲区的内容,将其追加到模式缓冲区 |
l |
列出非打印字符 |
p |
打印行 |
n |
读入下一行输入,并从下一条而不是第一条命令对其处理 |
q |
结束或退出sed |
r |
从文件中读取输入行 |
! |
对所选行以外的行应用所有命令 |
s |
用一个字符串替换另外一个字符串 |
替换标志:
g |
在行内进行全局替换 |
p |
打印行 |
w |
将行写入文件 |
x |
交换暂存缓冲区和模式空间的内容 |
y |
将字符转换成另外一个字符 |
sed例子:
打印:p命令
sed ‘/abc/p’ file |
打印file中包含abc的行。默认情况sed把所有行都打印到屏幕,如果某行匹配到模式,则把该行另外再打印一遍
|
sed -n ‘/abc/p’ file |
和上面一样,只是去掉了sed的默认行为,只会打印匹配的行 |
删除:d命令
sed ‘3,$d’ file |
删除从第3行到最后一行的内容。 |
sed ‘$d’ file |
删除最后一行的内容 |
sed ‘/abc/d’ |
删除包含abc的行。 |
sed ‘3d’ file |
删除第三行的内容 |
替换:s命令
sed ‘s/abc/def/g’ file |
把行内的所有abc替换成def,如果没有g,则只替换行内的第一个abc |
sed -n ‘s/abc/def/p’ file |
只打印发生替换的那些行 |
sed ‘s/abc/&def/’ file |
在所有的abc后面添加def(&表示匹配的内容) |
sed -n ‘s/abc/def/gp’ file |
把所有的abc替换成def,并打印发生替换的那些行 |
sed ‘s#abc#def#g’ file |
把所有的abc替换成def,跟在替换s后面的字符就是查找串和
替换串之间的分割字符,本例中试# |
指定行的范围:逗号
sed -n ‘/abc/,/def/p’ file |
打印模式abc到def的行 |
sed -n ‘5/,/def/p’ file |
打印从第五行到包含def行之间的行。 |
sed /abd/,/def/s/aaa/bbb/g |
修改从模式abc到模式def之间的行,把aaa替换成def |
多重编辑-e
sed -e ‘1,3d’ -e ‘s/abc/def/g’ file |
删除1-3行,然后把其余行的abc替换成def |
读文件:r命令
sed ‘/abc/r newfile’ file |
在包含abc的行后读入newfile的内容 |
写文件:w命令
sed ‘/abc/w newfile’ file |
在包含abc的行写入newfile |
追加:a命令
sed ‘/abc/a\def’ file |
在包含abc的行后新起一行,写入def |
插入:i命令
sed ‘/abc/i\def’ file |
在包含abc的行前新起一行,写入def |
修改:c命令
sed ‘/abc/c\def’ file |
在包含abc的行替换成def,旧文本被覆盖 |
读取下一行:n命令
sed ‘/abc/{n ; s/aaa/bbb/g;}’ file |
读取包含abc的行的下一行,替换aaa为bbb |
转换:y命令
sed ‘y/abc/ABC’ file |
将a替换成A,b替换成B,c替换成C(正则表达式元字符不起作用) |
退出:q命令
sed ‘/abc/{ s/aaa/bbb/ ;q; }’ file |
在某行包含了abc,把aaa替换成bbb,然后退出sed。 |
暂存和取用:h命令(把模式行存储到暂存缓冲区)和g(取出暂存缓冲区的行并覆盖模式缓冲区)G(取出临时缓冲区的行)命令
h和g是复制行为(覆盖),H和G表示追加。
sed -e ‘/abc/h’ -e ‘$G’ file |
包含abc的行通过h命令保存到暂存缓冲区,在第二条命令汇中,sed读到最后一行$时,G命令从暂存缓冲区中读取一行,追加到模式缓冲区的后面。即所有包含abc的行的最后一行被复制到文件末尾。 |
sed -e ‘/abc/{h; d;}’ -e ‘/def/{g; }’ file |
包含abc的行会移到包含def的行上,并进行覆盖。 |
暂存和互换:h和x命令
sed -e ‘/abc/h’ -e ‘/def/x’ file |
包含abc的行会被换成def的行。 |
-i选项:直接作用源文件,源文件将被修改。
分享到:
相关推荐
linux shell命令sed的使用方法
linux shell sed 使用详解 整理的很好。很不错的资源。写脚本全靠这个了。
Linux shell 用sed命令在文本的行尾或行首添加字符
shell编程中经常用到sed命令,本文详细介绍了shell常用命令sed详细用法
you will understand basic knowledge about sed command of linux shell script.
linux shell sed从入门到精通
linux shell Sed学习笔记.相信你看完会明白的
1 Linux主要shell命令详解 2 Linux bash shell脚本语法入门 3 Shell基本命令 4 使用 sed 编辑器
本文主要给大家介绍了关于Linux shell用sed批量更改文件名的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。 示例 去除特定字符 目标:将 2017-01-01.jpg、2018-01-01.jpg 改为 ...
this materials include some common sed usage, I hope it's helpfull to you.
Proper usage of shell commands can easily solve many complex tasks with a few lines of code, but most linux users don't have the right know-how to use the Linux shell to its full potential. ...
Proper usage of shell commands can easily solve many complex tasks with a few lines of code, but most linux users don't have the right know-how to use the Linux shell to its full potential. ...
第11章 Linux/UNIX Shell类型与区别 273 第12章 子Shell与进程处理 288 第13章 函数 314 第14章 别名、列表及数组 335 第15章 一些混杂的主题 359 第16章 Shell脚本调试技术 403 第17章 bash Shell编程范例 ...
Proper usage of shell commands can easily solve many complex tasks with a few lines of code, but most linux users don't have the right know-how to use the Linux shell to its full potential. ...
11.Shell编程之SED及GREP综合讲解.mp4 12.Shell编程四剑客及案例详解.mp4 13.Linux下自动化运维企业案例.mp4 14.Linux系统增量备份脚本.mp4 15.自动收集服务器硬件系统信息脚本.mp4 16.Shell编程之磁盘监控报警脚本_...
Learning Linux Shell Scripting 2nd Edition pdf Break through the practice of writing tedious code with shell scripts Key Features Learn to impeccably build shell scripts and develop advanced ...
linux下常用脚本命令集合 : awk sed bash shell Shell范例精解(第4版)