在前面都已经知道了d、p、-n、s///g等的用法。
---------------------------------------------------
sed中,a命令是指在匹配的位置后面插入新的内容。
c命令是说将在匹配模式空间的指定行用新文本加以替代。
看例子:
zoer@ubuntu:~$ cat data
naughty is a boy
cc is a girl
zoer is naughty
zoer@ubuntu:~$ cat s
/^nau/a\
naughty\
is\
a\
boy
zoer@ubuntu:~$ sed -f s data
naughty is a boy
naughty
is
a
boy
cc is a girl
zoer is naughty
上面例子是a命令的例子。在以nau开头的行下面插入一些内容。
看c命令。
zoer@ubuntu:~$ cat s
/^nau/c\
naughty\
is\
a\
boy
zoer@ubuntu:~$ cat data
naughty is a boy
cc is a girl
zoer is naughty
zoer@ubuntu:~$ sed -f s data
naughty
is
a
boy
cc is a girl
zoer is naughty
例子仅仅把a改成了c。结果就是以nau开头的行,被替换成了相应的内容。
-------------------------------------
q命令是说匹配到一定的行之后退出sed命令。
zoer@ubuntu:~$ cat data
cc is a girl
zoer is naughty
naughty loves cc.
cc loves naughty
zoer@ubuntu:~$ cat s
/^nau/q
zoer@ubuntu:~$ sed -f s data
cc is a girl
zoer is naughty
naughty loves cc.
zoer@ubuntu:~$
上面的例子,匹配到以nau开头的行,sed命令就终止了。