深入解析Linux 常用命令--sed

1.概述

sed是一个功能十分强大的文本处理命令,本文主要介绍一些常用的文本处理方法。

2.总述

2.1sed命令行格式

​ sed [-nefri] ‘command’ 输入文本/文件

2.2 常用选项

​ -n∶取消默认的输出,使用安静(silent)模式。

    -e∶进行多项编辑,即对输入行应用多条sed命令时使用. 直接在指令列模式上进行 sed 的动作编辑

    -f∶指定sed脚本的文件名. 直接将 sed 的动作写在一个档案内, -f filename 则可以执行 filename 内的sed 动作

    -r∶sed 的动作支援的是延伸型正则表达式的语法。(预设是基础正则表达式语法)

    -i∶直接修改读取的文件内容,而不是由屏幕输出       

2.3 常用命令

p∶ 列印,亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起用

​ i ∶ 插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行)

​ a ∶ 新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)

​ d ∶ 删除,因为是删除,所以 d 后面通常不接任何内容

​ c ∶ 取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行

​ s∶ 取代,可以直接进行替换的工作。通常这个 s 的动作可以搭配正则表达式。例如 1,20s/old/new/g

​ / /: 模式匹配

3.各选项用法

3.1 -i

直接修改文件,不带该选项是不会操作文件,只会显示处理结果


[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart root: hello

Apr 21 15:34:59 smart root: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

[root@smart Desktop]# sed "s/root/user/g" test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart root: hello

Apr 21 15:34:59 smart root: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

[root@smart Desktop]# sed -i "s/root/user/g" test.log

[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15

3.2 -n

只显示操作结果,而不显示中间过程,即不显示文本内容。


[root@smart Desktop]# sed "/user/p" test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

[root@smart Desktop]# sed -n "/user/p" test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

3.3 p

​ 查询结果打印输出,如打印1,2行


[root@smart Desktop]# sed -n "1,2p" test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

[root@smart Desktop]# sed -n "/user/,/15:45:55/p" test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

3.4 a/i

添加内容


#在第一行后添加hello world

[root@smart Desktop]# sed  "1a hello world" test.log

Apr 21 15:34:49 smart user: hello

hello world

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

#在每一行添加hello world

[root@smart Desktop]# sed  "a hello world" test.log

Apr 21 15:34:49 smart user: hello

hello world

Apr 21 15:34:59 smart user: hello2

hello world

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

hello world

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

hello world

#第一行前插入

[root@smart Desktop]# sed  "1i hello world" test.log

hello world

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

#最后一行插入

[root@smart Desktop]# sed '$a bye' test.log

Apr 21 15:34:49 smart user: hello

$ * 

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

bye

3.5 d

删除指定内容


[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

[root@smart Desktop]# sed '1d' test.log 

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

删除包含user到第15:45:55行的内容


[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

[root@smart Desktop]# 

[root@smart Desktop]# sed  "/user/,/15:45:55/d" test.log

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

[root@smart Desktop]# 

3.6 模式匹配查询


root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

[root@smart Desktop]# sed -n '/hello2/' test.log

sed: -e expression #1, char 8: missing command

[root@smart Desktop]# sed -n '/hello2/p' test.log

Apr 21 15:34:59 smart user: hello2


3.7 替换一行或多行 c

替换1,2行为hello


[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

[root@smart Desktop]# sed -n '1,2c hello' test.log

hello

[root@smart Desktop]# sed  '1,2c hello' test.log

hello

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 


3.8 替换字符串 s

将smart替换为juyin,常加上g表示全局替换


[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

[root@smart Desktop]# sed  "s/smart/juyin/" test.log

Apr 21 15:34:49 juyin user: hello

Apr 21 15:34:59 juyin user: hello2

Apr 22 15:45:55 juyin test1.bin: test1.c main 15 

Apr 22 15:46:08 juyin test1.bin: test1.c main 15 

[root@smart Desktop]# sed  "s/smart/juyin/g" test.log

Apr 21 15:34:49 juyin user: hello

Apr 21 15:34:59 juyin user: hello2

Apr 22 15:45:55 juyin test1.bin: test1.c main 15 

Apr 22 15:46:08 juyin test1.bin: test1.c main 15 


3.9 显示文件行号 =


[root@smart Desktop]# sed -n "/smart/=" test.log

1

3

4

5

3.10 sed 结果写入到文件w


#l表示显示特殊字符

[root@smart Desktop]# sed -n "/smart/l" test.log

Apr 21 15:34:49 smart user: hello$

Apr 21 15:34:59 smart user: hello2$

Apr 22 15:45:55 smart test1.bin: test1.c main 15 $

Apr 22 15:46:08 smart test1.bin: test1.c main 15 $

[root@smart Desktop]# sed -n "/user/ w test.log2" test.log

[root@smart Desktop]# cat test.log2

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

[root@smart Desktop]# 

3.11 替换路径

[root@smart Desktop]# pwd

/root/Desktop

[root@smart Desktop]# pwd|sed “s? PWD? P W D ? {PWD}/test?g”

/root/Desktop/test

4 注意事项

对于特殊字符的操作需要注意

* 代表任意多个

^ 任意开头 ^[1-9]任意1-9开头的数字

$ 行结束符

\ 转义字符

. 任意字符


[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

[root@smart Desktop]# sed "s/^smart/abc/g" test.log

Apr 21 15:34:49 smart user: hello

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

[root@smart Desktop]# sed "s/^Apr/abc/g" test.log

abc 21 15:34:49 smart user: hello

abc 21 15:34:59 smart user: hello2

abc 22 15:45:55 smart test1.bin: test1.c main 15 

abc 22 15:46:08 smart test1.bin: test1.c main 15 

[root@smart Desktop]# sed "s/user.*/abc/g" test.log

Apr 21 15:34:49 smart abc

Apr 21 15:34:59 smart abc

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

[root@smart Desktop]# sed "s/^.*$/abc/g" test.log

abc

abc

abc

abc

[root@smart Desktop]# sed -i "1a \$ \* " test.log

[root@smart Desktop]# cat test.log

Apr 21 15:34:49 smart user: hello

$ * 

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

[root@smart Desktop]# sed -n "/\$/p" test.log

Apr 21 15:34:49 smart user: hello

$ * 

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 

[root@smart Desktop]# sed -n "/\*/p" test.log

$ * 

[root@smart Desktop]# sed -n "#\$#p" test.log

[root@smart Desktop]# sed -n "#$#p" test.log

[root@smart Desktop]# sed -n "s#test#abc#g" test.log

[root@smart Desktop]# sed  "s#test#abc#g" test.log

Apr 21 15:34:49 smart user: hello

$ * 

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart abc1.bin: abc1.c main 15 

Apr 22 15:46:08 smart abc1.bin: abc1.c main 15 

[root@smart Desktop]# sed "#$#p" test.log

Apr 21 15:34:49 smart user: hello

$ * 

Apr 21 15:34:59 smart user: hello2

Apr 22 15:45:55 smart test1.bin: test1.c main 15 

Apr 22 15:46:08 smart test1.bin: test1.c main 15 



Juyin@2018/4/28,欢迎留言交流

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值