###补充
^.*$ 整行
sed
sed [-nefri] [动作]
-n:只列出经过sed特殊处理过的行
-e:可以在同一行里执行多条命令
-f:将sed的动作写入一个文件内,
== -r:支持扩展型正则表达式的语法(可以不脱义使用?,+,|,(),类似于grep -E==
-i:直接修改读取的文件内容,而不是有屏幕输出
sed -n ‘5’p test.txt 不加-n会打印所有的内容,并把匹配行输出2次。
==sed 中使用外部变量,可以把单引号换成双引号 sed -i =="&s/./IPADDR=
i
p
a
d
d
r
/
"
s
e
d
−
n
′
1
,
5
′
p
t
e
s
t
.
t
x
t
s
e
d
−
n
′
1
,
ipaddr/" sed -n '1,5'p test.txt sed -n '1,
ipaddr/"sed−n′1,5′ptest.txtsed−n′1,'p test.txt ==$表示尾行
sed -n '/root/'p test.txt
sed -n '/^1/'p test.txt
sed -n ‘inKaTeX parse error: Expected 'EOF', got '#' at position 200: …est.txt sed 's#̲ot#to#g' test.t…/123&/’ test.txt
sed -i ‘s/ot/to/g’ test.txt
替换:sed ‘1,10s/nologdin/login/g’ 1.txt (g全局替换)
sed ‘s#^.KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲&login#g' 1.txt…/123&/’ test.txt 在每一行的前面加上123
sed 's#[^0-9a-zA-Z]##g’ 1.txt 把非数字字母的替换为空。
sed -r ‘s#(1+)(:.?(.$)#\3\2\1#g’ 1.txt
sed -n ‘/user/p; /aming/p’ 1.txt
sed -n -e ‘/user/p’ -e ‘/aming/p’ 1.txt
sed -n -r ‘/usr|aming/p’ 1.txt
注释: sed '30,40s/^/##/ 1.txt
sed -r ‘s/([^:]+)(:.*: )([^:]+)/\3\2\1/’ /etc/passwd 匹配第一段除":“的任意字符 匹配第二段”:任意字符: “第三段 也是除开”:"的任意字符? \3\2\1 相当于把第一段和第三段对调
sed 使用外部变量,比如a=1,sed "/abc/
a
/
"
1.
t
x
t
需
要
使
用
双
引
号
s
e
d
′
1
,
5
s
/
.
∗
a/" 1.txt 需要使用双引号 sed '1,5s/^.*
a/"1.txt需要使用双引号sed′1,5s/.∗/aaa&/’ 1.txt 和 sed ‘1,5s/^.*$/&aaa/’ 1.txt
sed ‘/ling/i hello world\nxiang’ 1.txt 匹配到ling,在前面增加hello world和xiang两行
sed ‘/ling/a hello world\nxiang’ 1.txt 匹配到ling,在后面增加hello world和xiang两行
sed ‘1,5 i hello world’ 1.txt
sed ‘1,5 a hello world’ 1.txt
a-z0-9 ↩︎