说明:
[root@k8s-master test1]# cat test.txt
hello1
world
hello1world
hello1world221hello
this is a test!
^ 开始
$ 结束
. 任意一个
* 0个或多个
[] 指定范围中
[^] 不在指定范围中
\(..\)保存匹配的字符,后面用数字表式
[root@k8s-master test1]# sed 's/\(world\)/\1ers/' test.txt
hello1
worlders
hello1worlders
hello1worlders221hello
this is a test!
& 保存搜索字符,用来替换其他字符
[root@k8s-master test1]# sed 's/world/***&***/' test.txt
hello1
***world***
hello1***world***
hello1***world***221hello
this is a test!
\< 锚定单词开始
\> 锚定单词结束
[root@k8s-master test1]# sed '/world\>/p' test.txt -n
world
hello1world
[root@k8s-master test1]# sed '/world\</p' test.txt -n
[root@k8s-master test1]# sed '/\<world/p' test.txt -n
world
x\{m\} 重复x字符m次
x\{m,n\} 重复x字符,至少m,至多n次
x\{m,\} 重复x字符至少m次
[root@k8s-master test1]# sed '/abc\{2,\}/p' ./test1.txt -n
abcccdef
[root@k8s-master test1]# cat test1.txt
abc
abcabc
def
abcdef
abcccdef
[root@k8s-master test1]# sed '/\(abc\)\{2,\}/p' ./test1.txt -n
abcabc
缩写说明
c: change
a: append
i: insert
d: delete
替换
[root@k8s-master test1]# sed 's/hello/hello1/' test.txt
hello11
world
hello11world
hello11world221hello
this is a test!
[root@k8s-master test1]# sed 's/hello/hello1/g' test.txt
hello11
world
hello11world
hello11world221hello1
this is a test!
[root@k8s-master test1]# cat test.txt
hello1
world
hello1world
hello1world221hello
this is a test!
[root@k8s-master test1]# cat test.txt
hello
world
helloworld
this is a test!
[root@k8s-master test1]# sed -e '2,3p' -n ./test.txt
world
helloworld
[root@k8s-master test1]# sed -e '2,3p' -n ./test.txt
world
helloworld
[root@k8s-master test1]# sed -e '3ahello-world-3a' ./test.txt
hello
world
helloworld
hello-world-3a
this is a test!
[root@k8s-master test1]# sed -e '3ihello-world-3i' ./test.txt
hello
world
hello-world-3i
helloworld
this is a test!
[root@k8s-master test1]# sed -e '2,4d' ./test.txt
hello
[root@k8s-master test1]# sed -e '3,$d' ./test.txt
hello
world
[root@k8s-master test1]# sed -e '2a first\
> second' test.txt
hello
world
first
second
helloworld
this is a test!
[root@k8s-master test1]# sed -e '2,3c second-third' test.txt
hello
second-third
this is a test!
[root@k8s-master test1]# sed '/hello/p' ./test.txt
hello
hello
world
helloworld
helloworld
this is a test!
[root@k8s-master test1]# sed '/hello/p' ./test.txt -n
hello
helloworld
[root@k8s-master test1]# sed '/hello/d' ./test.txt
world
this is a test!
[root@k8s-master test1]# sed '/hello/d' ./test.txt -n
[root@k8s-master test1]#
[root@k8s-master test1]# sed 's/hello/world1/g' test.txt
world1
world
world1world
this is a test!
[root@k8s-master test1]#
# 文件查找和替换
[root@k8s-master test1]# sed -e 's/hello/hell1/' test.txt
hell1
world
hell1world
hell1world221
this is a test!
[root@k8s-master test1]# sed -e 's/hello/hello1/' test.txt
hello1
world
hello1world
hello1world221
this is a test!
[root@k8s-master test1]# cat test.txt
hello
world
helloworld
helloworld221
this is a test!
[root@k8s-master test1]# sed -i 's/hello/hello1/' test.txt
[root@k8s-master test1]# cat test.txt
hello1
world
hello1world
hello1world221
this is a test!