linux sed

25 篇文章 0 订阅

说明:

[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!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值