sed编辑器

数据文件lines

Line one.
The second line.
The third.
This is line four.
Five.
This is the sixth sentence.
This is line seven.
Eighth and last.
1. -n选项,sed仅仅在标准输出上输出特定的行
qixuan@ubuntu:~/qixuan02$ sed -n '/line/ p' lines
The second line.
This is line four.
This is line seven.
qixuan@ubuntu:~/qixuan02$ sed -n '3,6 p' lines
The third.
This is line four.
Five.
This is the sixth sentence.
2. -f选项指示sed应该从该选项后面的文件中读取程序
qixuan@ubuntu:~/qixuan02$ vi print3_6
qixuan@ubuntu:~/qixuan02$ sed -n -f print3_6 lines
The third.
This is line four.
Five.
This is the sixth sentence.
qixuan@ubuntu:~/qixuan02$ cat lines 
Line one.
The second line.
The third.
This is line four.
Five.
This is the sixth sentence.
This is line seven.
Eighth and last.
3.insert程序:选定所有包含字符串This的行,然后在选定的行前面插入一个换行符和文本“BEFORE.".
qixuan@ubuntu:~/qixuan02$ cat insert_demo 
/This/ i\
BEFORE.
qixuan@ubuntu:~/qixuan02$ sed -f insert_demo lines 
Line one.
The second line.
The third.
BEFORE.
This is line four.
Five.
BEFORE.
This is the sixth sentence.
BEFORE.
This is line seven.
Eighth and last.
4.substitute程序:
qixuan@ubuntu:~/qixuan02$ cat subs_demo 
s/line/sentence/p
qixuan@ubuntu:~/qixuan02$ sed -n -f subs_demo lines 
The second sentence.
This is sentence four.
This is sentence seven.

5.next指令:

qixuan@ubuntu:~/qixuan02$ sed -n -f next_demo1 lines 
Line one.
The second line.
This is line four.
Five.
This is the sixth sentence.
This is line seven.
Eighth and last.
qixuan@ubuntu:~/qixuan02$ cat next_demo1 
3 n
p
6.next指令中,如果是大写的N的话,效果如下。   substitute指令用空格代替嵌入的换行符

qixuan@ubuntu:~/qixuan02$ cat next_demo3
/the/ N
s/\n/ /
p
qixuan@ubuntu:~/qixuan02$ sed -n -f next_demo3 lines 
Line one.
The second line.
The third.
This is line four.
Five.
This is the sixth sentence. This is line seven.
Eighth and last.
7.接下来的示例演示sed如何同时使用多条指令

qixuan@ubuntu:~/qixuan02$ cat compound.in 
1. The words on this page...
2. The words on this page...
3. The words on this page...
4. The words on this page...
qixuan@ubuntu:~/qixuan02$ vi compound
qixuan@ubuntu:~/qixuan02$ cat compound
1,3 s/words/text/
2,4 s/text/TEXT/
3 d
qixuan@ubuntu:~/qixuan02$ sed -f compound compound.in
1. The text on this page...
2. The TEXT on this page...
4. The words on this page...
8.如果命令行中没有-n选项,sed程序将文件中的所有行显示一次。程序文件末尾的print指令会再次显示第三行

qixuan@ubuntu:~/qixuan02$ cat compound3
2 a\
This is line 2a.\
This is line 2b.
3 p
qixuan@ubuntu:~/qixuan02$ sed -n -f compound3 compound.in 
This is line 2a.
This is line 2b.
3. The words on this page...
qixuan@ubuntu:~/qixuan02$ sed -f compound3 compound.in 
1. The words on this page...
2. The words on this page...
This is line 2a.
This is line 2b.
3. The words on this page...
3. The words on this page...
4. The words on this page...

sed编辑器_第1张图片


你可能感兴趣的:(sed编辑器)