man sed
--------------------------------------------------------------------------------------------------
g:[address[,address]]g 将hold space中的内容拷贝到pattern space中,原来pattern space里的内容清除
G:[address[,address]]G 将hold space中的内容append到pattern space\n后
h:[address[,address]]h 将pattern space中的内容拷贝到hold space中,原来的hold space里的内容被清除
H:[address[,address]]H 将pattern space中的内容append到hold space\n后
d:[address[,address]]d 删除pattern中的所有行,并读入下一新行到pattern中
D:[address[,address]]D 删除multiline pattern中的第一行,不读入下一行
p: Print the current pattern space
P:Print up to the first embedded newline of the current pattern space.
D:If pattern space contains no newline, start a normal new cycle as if the d command was issued.
Otherwise, delete text in the pattern space up to the first newline, and restart cycle with
the resultant pattern space, without reading a new line of input.
N:Add a newline to the pattern space, then append the next line of input to the pattern space.
If there is no more input then sed exits without processing any more commands.
=: Print the current line number
a \text:
Append text, which has each embedded newline preceded by a backslash
i \text:
Insert text, which has each embedded newline preceded by a backslash
q [exit-code]:
Immediately quit the sed script without processing any more input, except
that if auto-print is not disabled the current pattern space will be printed
The exit code argument is a GNU extension
Q [exit-code]:
Immediately quit the sed script without processing any more input. This is
a GNU extension
r filename:
Append text read from filename
R filename:
Append a line read from filename. Each invocation of the command reads a line
from the file. This is a GNU extension
practical command
--------------------------------------------------------------------------------------------------
[root@peiyuan ABS]# cat data-for-sed
111111111
22222222
3333333
444444
55555
6666
777
88
9
1.添加行号
sed = data-for-sed | sed -e 'N;s/\n/ /g' -e 's/^\([0-9]\)/\[\1\]/'
[1] 111111111
[2] 22222222
[3] 3333333
[4] 444444
[5] 55555
[6] 6666
[7] 777
[8] 88
[9] 9
2.打印第3行到第7行
sed -n '3,7p' data-for-sed
3333333
444444
55555
6666
777
3.将匹配范围内的内容写入文件
sed -n '/3/,/7/w data-for-sed' data-for-sed-1
4.反向匹配
sed -n '/3/!p' data-for-sed
5.反向匹配替换
sed '/baz/!s/foo/bar/g' data-for-sed
6.插入文件内容
sed '/3/r data-for-sed-1' data-for-sed
7.插入在线内容
sed '3a\WANRING' data-for-sed
8.强制退出
sed '/WANRING/q' data-for-sed
9.文件内容倒置输出
sed '1!G;h;$!d' data-for-sed
10.删除每行的前后空白
sed 's/^[ \t]*//;s/[ \t]*$//' data-for-sed
11.整体右移
sed 's/^/ /' data-for-sed
12.整体左移
sed 's/^ //' data-for-sed
13.文件内容变为右对齐
sed -e :a -e 's/^.\{1,78\}$/ &/;ta'
14.文件内容居中
sed -e :a -e 's/^.\{1,78\}$/ & /;ta'
15.替换模式字符串的第N次出现
sed 's/222/***/N' data
16.替换模式字符串的第N次出现-打点计数法
/a/{ # start first pattern match
x # swap pattern-space and hold-space
s/^/./ # add one dot in hold-space
/^.\{3\}$/{ # if dot-number meets
x # swap pattern-space and hold-space
s/a/b/ # do substitution
b # jmp to end
}
x # swap
}' data
17.找出100以内,个位与十位相同的2位数-回溯引用
seq 100 | sed -n '/^\(.\)\1$/p'
18.大小写转换
sed -n 's/.*/\U&/p' # 全部转换为大写
sed -n 's/.*/\u&/p' # 首字母大写
sed -n 's/.*/\L&/p' # 全部小写
sed -n 's/.*/\l&/p' # 首字母小写
19.忽略大小写匹配
sed -n '/pattern/Ip'