您的位置:首页 > 系统 Sed编辑器
站内搜索

Sed编辑器

原帖及讨论:http://bbs.bccn.net/thread-208986-1-1.html

[gxlinux@localhost ~]$ cat sed
1.line one.
2.the second line.
3.the third.
4.this is line four.
5.five
6.this is the sixth sentence.
7.this is line seven.
8.eight and last.
[gxlinux@localhost ~]$ sed '/last/ p' sed #正则表达式
1.line one.
2.the second line.
3.the third.
4.this is line four.
5.five
6.this is the sixth sentence.
7.this is line seven.
8.eight and last.
8.eight and last.
[gxlinux@localhost ~]$ sed '/last/ p' sed -n
8.eight and last.
[gxlinux@localhost ~]$ sed '3,6 p' sed -n
3.the third.
4.this is line four.
5.five
6.this is the sixth sentence.
[gxlinux@localhost ~]$ sed '3,6 !p' sed -n
1.line one.
2.the second line.
7.this is line seven.
8.eight and last.
[gxlinux@localhost ~]$ sed '$ p' sed -n
8.eight and last.
[gxlinux@localhost ~]$ sed '3 q' sed
1.line one.
2.the second line.
3.the third.
[gxlinux@localhost ~]$ cat>print3
3,6 p
[gxlinux@localhost ~]$ sed -n -f print3 sed
3.the third.
4.this is line four.
5.five
6.this is the sixth sentence.
[gxlinux@localhost ~]$ cat zhiling
/one/ i/
first
/second/ a/
two
3,4 c/
gxl
linux
s/last/fedora/
[gxlinux@localhost ~]$ sed -f zhiling sed
first
1.line one.
2.the second line.
two
gxl
linux
5.five
6.this is the sixth sentence.
7.this is line seven.
8.eight and fedora.
[gxlinux@localhost ~]$ sed 's/last/fedora/p' -n sed
8.eight and fedora.
[gxlinux@localhost ~]$ sed 's/last/fedora/w temp' sed
1.line one.
2.the second line.
3.the third.
4.this is line four.
5.five
6.this is the sixth sentence.
7.this is line seven.
8.eight and fedora.
[gxlinux@localhost ~]$ cat temp
8.eight and fedora.

[gxlinux@localhost ~]$ cat>zhiling
2,$g #从第二行开始处理,将一个换行符和hold区中的内容追加到pattern区中之后(h与g相反,x交换pattern区与hold区内容)
h
$!d #不是最后一行 删除
#所有命令都是工作在pattern区,并且是一行一行的处理,读取一行到pattern区然后将pattern区中的内容显示出来
#读取第一行到pattern区,将pattern区中的内容复制到hold区,删除pattern区内容(hold区中为第一行)
#读取第二行到pattern区,将一个换行符和hold区中的内容追加pattern区中,然后将其存储到hold区中,删除pattern区内容(hold区中为第二行加第一行)
#读取第八行到pattern区,将一个换行符和hold区中的内容追加pattern区中,然后将其存储到hold区中(此时pattern区内容就是倒过来的了,然后输出)
[gxlinux@localhost ~]$ sed -f zhiling sed
8.eight and last.
7.this is line seven.
6.this is the sixth sentence.
5.five
4.this is line four.
3.the third.
2.the second line.
1.line one.

[gxlinux@localhost ~]$ cat>zhiling
h #将pattern区中的内容复制到hold区中,hold区中原有内容丢失
n #跳过,读取下一行
p
g #与h指令相反
p
#首先读入一行并将它存入到hold区,然后读入下一行并显示,再获取先前的行并显示它,接着处理后续两行
[gxlinux@localhost ~]$ sed -nf zhiling sed
2.the second line.
1.line one.
4.this is line four.
3.the third.
6.this is the sixth sentence.
5.five
8.eight and last.
7.this is line seven.

 

  • 上一篇:find命令简述
  • 下一篇:LINUX VMWare-TOOLS的安装