linux sed

linux三剑客之sed

sed

sed 是linux中,流媒体编辑器.

grep : 过滤文本
sed  : 修改文本
awk  : 处理文本

1.sed 格式
sed [参数] '处理规则' [操作对象]

2.参数
-e : 允许多项编辑
指定行数多重删除'搭配参数-e'
[root@localhost ~]# sed -e '2d' -e '10d' 4.txt

-n : 取消默认输出
只显示新增加行'搭配参数-n'
[root@localhost ~]# sed -n '2p' 4.txt

-i : 就地编辑
将第十行复制这个动作写入到 4.txt 文件中并保存
[root@localhost ~]# sed  -i '2p' 4.txt

-r : 支持拓展正则
-f : 指定sed匹配规则脚本(简单来说指定一个文本的正则让另一个文件执行)
# 下面详解

-r,f支持拓展正则

[root@localhost ~]# cat 6.txt
12
21
32
43
54
65
76
[root@localhost ~]# sed -r '/2/d' 6.txt
43
54
65
76
'/2/d' 正则 
或者(将匹配规则写入一个文件当中,通过sed -f 指定 规则文件 操作 操作对象)
[root@localhost ~]# cat 6.txt
12
21
32
43
54
65
76
[root@localhost ~]# vim r.txt
vim r.txt
/12/d
[root@localhost ~]# sed -f r.txt 6.txt
21
32
43
54
65
76

sed定位之数字定位法

指定行号
sed '3d' 4.txt (精准至单独行)
sed '2,3d' 4.txt (规定范围)

sed定位之正则定位法

指定正则
sed '/g/d' 2.txt (带g的行数都删除)
sed '/^g/d' 2.txt (删除g开头的行数)

sed定位之数字和正则定位法

 sed '3,/^g/d' 2.txt (删除第三行到已g开头的行)

sed定位之正则正则定位法

sed '/^g/,/^j/d' (从g开头的行数一致删除到以^j开头的行数(包括g和j开头的行))

sed编辑模式

d : 删除
p : 打印
a : 在当前行后添加一行或多行
sed '2axxx' 4.txt 

c :用新文本修改(替换)当前行
sed '2cxxx' 4.txt

i : 在当前行之前,插入文本(单独使用时)
sed '2ixxx' 4.txt

r : 在文件中读内容
sed '2r r.txt' 2.txt 

w : 将指定行写入文件
sed '2w w.txt' 2.txt 

y : 将字符转换成另一个字符
sed '2y/fa/FA/' 2.txt 

s : 将字符串转换成另一个字符串(每一行只替换一次)
sed 's/11/22/' 6.txt 
g : 全部执行
sed 's/11/22/g' 6.txt 
i : 忽略大小写(跟 s 模式一起使用时)
# sed 's/123/000/gi' 1.txsed 's/123/000/gi' 1.txit

& :代表前面匹配到的内容


sed编辑模式之d(删除)

sed 编辑模式
	d : 删除
	
指定单行删除
[root@localhost ~]# sed '3d' 4.txt
定位删除(指定范围)
[root@localhost ~]# sed '3,9d' 4.txt

sed编辑模式之p(打印)


  p : 打印
  
vim 4.txt
2222
3333
4444

'将对应行向下复制一行'
[root@localhost ~]# sed '2p' 4.txt
vim 4.txt
2222
3333
3333
4444

sed编辑模式之a(某行之后添加一行指定的文件内容)

[root@localhost ~]# cat 6.txt 
12
21
32
43
54
65
76
[root@localhost ~]# sed '1all' 6.txt
12
ll
21
32
43
54
65
76

sed编辑模式之c(用新文本修改(替换)当前行)

[root@localhost ~]# cat 6.txt 
12
21
32
43
54
65
76
[root@localhost ~]# sed '1chaha' 6.txt 
haha
21
32
43
54
65
76
[root@localhost ~]# 

sed编辑模式之i(在当前行的上一行插入指定文本内容)

[root@localhost ~]# cat 6.txt 
12
21
32
43
54
65
76
[root@localhost ~]# sed '1ioo' 6.txt 
oo
12
21
32
43
54
65
76
您在 /var/spool/mail/root 中有新邮件
[root@localhost ~]# 

sed编辑模式之r(在文件中读内容)

[root@localhost ~]# cat r.txt
/12/d
[root@localhost ~]# cat 6.txt
12
21
32
43
54
65
76
[root@localhost ~]# sed '2r 6.txt' r.txt
/12/d (只有一行,所以无法插入到第二行)
[root@localhost ~]# sed '1r 6.txt' r.txt
/12/d
12
21
32
43
54
65
76

sed编辑模式之w(将指定行写入文件)

w的结构
	sed '1(行数)w(写入) z.txt(新建文件)' 2.txt(写入文本内容来源)
[root@localhost ~]# ll
总用量 24
-rw-r--r-- 1 root root  92 12月 21 15:11 1.txt
-rw-r--r-- 1 root root 360 12月 21 15:26 2.txt
-rw-r--r-- 1 root root 333 12月 21 15:32 3.txt
-rw-r--r-- 1 root root 360 12月 21 19:05 4.txt
-rw-r--r-- 1 root root  35 12月 21 16:20 5.txt
-rw-r--r-- 1 root root   0 12月 21 20:19 6.txt
-rw-r--r-- 1 root root   0 12月 21 20:20 a.txt
-rw-r--r-- 1 root root   6 12月 21 19:42 r.txt
[root@localhost ~]# cat 2.txt
|k|j|b|s|f|b|f|b|b|v|a|b|j|b|a|k
|g|l|k|a|b|a|k|b|k|j|j|k|a|b|d|f
|s|v|k|b|v|b|v|n|b|v|k|b|i|u|b|g
[root@localhost ~]# sed '1w z.txt' 2.txt
|k|j|b|s|f|b|f|b|b|v|a|b|j|b|a|k
|g|l|k|a|b|a|k|b|k|j|j|k|a|b|d|f
|s|v|k|b|v|b|v|n|b|v|k|b|i|u|b|g
[root@localhost ~]# cat z.txt
|k|j|b|s|f|b|f|b|b|v|a|b|j|b|a|k

sed编辑模式之y(将字符转换成另一个字符)

[root@localhost ~]# cat 5.txt
1234    9999
2234    9999
3       9999
4       99999
[root@localhost ~]# sed '1y/1/9/' 5.txt
9234    9999
2234    9999
3       9999
4       99999
(单独的字符对应转换)

sed编辑模式之s(将字符串转换成另一个字符)(每行之替换一次)

[root@localhost ~]# cat 1.txt 
123
234
345
456
678
890
1
2
3
4
5
6

[root@localhost ~]# sed 's/1/2' 1.txt
sed:-e 表达式 #1,字符 5:未终止的“s”命令
[root@localhost ~]# sed 's/1/2/' 1.txt
223
234
345
456
678
890
2
2
3
4
5
6

或
[root@localhost ~]# sed 's/123/000/' 1.txt
000
234
345
456
678
890
1
2
3
4
5
6

[root@localhost ~]# 

sed编辑模式之g(全部执行)(一行全部执行)

[root@localhost ~]# cat 1.txt 
123
234
345
456
678
890
1
2
3
4
5
6
[root@localhost ~]# sed 's/123/000/g' 1.txt
000
234
345
456
678
890
1
2
3
4
5
6
# 精准匹配123

案例应用

1、将nginx.conf中的注释行全部去掉

	[root@localhost ~]# sed '/^ *#/d' /etc/nginx/nginx.conf

2、将nginx.conf中每一行之前增加注释
	[root@localhost ~]# sed 's/.*/# &/g' /etc/nginx/nginx.conf

可以将&当做变量名指向.*(正则)

3、要求一键修改本机的ip,  192.168.15.100 ---> 192.168.15.101  172.16.1.100   ---> 172.16.1.101  
sed -i 's#.100#.101#g' /etc/sysconfig/network-scripts/ifcfg-eth[01]  

4、将/etc/passwd中的root修改成ROOT  sed -i 's#root#ROOT#g' /etc/passwd
posted @ 2021-12-21 20:57  谢俊杰  阅读(71)  评论(0编辑  收藏  举报