sed Wikipedia:
http://en.wikipedia.org/wiki/Sed
GNU sed manual:
http://www.gnu.org/software/sed/manual/sed.html
Sed - An Introduction and Tutorial by Bruce Barnett:
http://www.grymoire.com/Unix/Sed.html
如果要对[addresses]应用多个命令,除了用多个-e 外(sed -e 'addresses command1' -e 'addresses command2' file),还可用一对大括号把多个命令包围起来(最好把右边的大括号放到单独的一行中,这样兼容性更好)。
[addresses] [!] {
command1 [arguments]
command2 [arguments]
command3 [arguments]
对于每个命令还可以单独指定一个[addresses],每个 command 的限定范围同时满足外层的[addresses]和内层[addresses],如下所示:
[addresses] [!] {
[addresses] [!] command1 [arguments]
[addresses] [!] command2 [arguments]
[addresses] [!] command3 [arguments]
first~step
matches every stepth line starting with line first. For example, odd-numbered lines:
1~2
.
the last line of the last file of input.
/regexp/
any line which matches the regular expression regexp. The empty regular expression
//
repeats the last regular expression match.
\%regexp%
is same as
/regexp/
(just choose a different delimiter), the
%
may be replaced by any other single character.
/regexp/I
case-insensitive.
/regexp/M
causes
^
and
$
to match respectively (in addition to the normal behavior) the empty string after a newline, and the empty string before a newline. There are special character sequences (
\`
and
\'
) which always match the beginning or the end of the buffer. M stands for multi-line.
address1, address2
lines starting from where address1 matches, and continues until address2 matches (inclusively).
address1, +N
matches address1 and the N lines following address1.
address1, ~N
matches address1 and the lines following addr1 until the next line whose input line number is a multiple of N.
Execute whatever is available in the pattern space as a shell command, and the output will be returned to the pattern space.
$ sed -e 's/^/ls -l /e' -e 's/root/cig01/g' files.txt
-rw-r--r-- 1 cig01 cig01 201 Apr 13 20:47 /etc/shells
-rw-r--r-- 1 cig01 cig01 7634 Apr 13 20:47 /etc/profile
# 即把之前备份的上一行放入到pattern space,把当前行备份到hold space
# 检查pattern space是不是ABCD,或不是则用i在Output Stream中插入ABCD
/ABCD/ !{
# 恢复当前行,下行不用x,用g也可以
The "n" command will print out the current pattern space (unless the "-n" flag is used), empty the current pattern space, and read in the next line of input.
The "N" command does not print out the current pattern space and does not empty the pattern space. It reads in the next line, but appends a new line character along with the input line itself to the pattern space.
The "d" command deletes the current pattern space, reads in the next line, puts the new line into the pattern space, and aborts the current command, and starts execution at the first sed command. This is called starting a new "cycle."
The "D" command deletes the first portion of the pattern space, up to the new line character, leaving the rest of the pattern alone.
$ ! {
# If its not the last line of file
# Append the next line with the pattern space delimited by \n.
# Jump to the loop again.
b loop
# Now all the lines will be available in pattern space delimited by newline.
# Substitute all the occurrence of data between "" with the empty.
s/\"[^\"]*\"//g
' file
:loop
# Check if the line ends with the backslash (/\\$/),
# if yes, read and append the next line to pattern space.
/\\$/ N
# Substitute the \ at the end of the line and number of spaces
# followed by that, with the single space.
s/\\\n */ /
# The branch will be executed only if substitution is success.
t loop
' file