shell中的文本三剑客—sed
基本知识了解
sed是一种流编辑器,它可以用于对文本进行替换、删除、插入、替换等操作。它通常用于处理文本文件中的数据,例如日志文件、配置文件等。
sed命令的基本语法如下:
sed [选项] '动作' 文件名
其中,选项包括:
-n:只输出处理过程中的结果,不输出输入的内容。
-e:支持多个命令的同时执行,可以用分号隔开。一般用;代替
-f:从文件中读取sed命令,可以指定一个或多个文件。
-i:直接修改原始文件,而不是输出到终端--->可以在-i后边加个后缀 生成新的文件
-r:启用扩展正则表达式,不需要对特殊字符进行转义。
-s:替换操作只作用于每行中的第一个匹配项。
-u:不缓冲输出,每次输出都会立即刷新到屏幕上。
-z:将输入文件视为一个单一的字符串,而不是多行文本。
动作(命令)可以是:
s:替换操作,格式为------> s/要替换的字符串/替换成的字符串/。
d:删除操作,删除匹配到的行。
p:打印操作,打印出匹配到的行。
a:插入操作,在指定行后插入文本。
i:插入操作,在指定行前插入文本。
c: 整行替换
= : 输出行号
p: 输出多少行的内容
g:全局替换
默认情况替换第一个
+g 之后替换所有的
2 替换第二个 sed -i '/zhangyi/ s/liuna/hehaotian/2' hehaotian.txt
2g 从第二个开始替换
-n 压制 :不要自动显示出现文本空间里的内容
pattern space 模式空间
hold space 保留空间
-p 是sed内部的命令 输出内容到屏幕 print
/longyi/ p 查找到内容包含字符串longyi的行,然后显示出来 ---->默认是整行输出
举几个例子
1> 要将文件中的所有"hello"替换成"world",可以使用以下命令:
sed 's/hello/world/g' file.txt
其中,g表示全局替换,即将每一行中所有匹配到的"hello"都替换成"world"。
2> 要删除文件中所有包含"error"的行,可以使用以下命令:
sed '/error/d' file.txt
其中,/error/表示匹配包含"error"的行,d表示删除匹配到的行。
3> 要在文件的第10行后插入一行文本"hello world",可以使用以下命令:
sed '10a hello world' file.txt
其中,10表示要在第10行后插入文本,a表示插入操作。
进阶知识点
标签
\1
用括号括起来
小括号
每一个
\w ---->[0-z]
前面是模式匹配 后面是替换
引用后向引用:在替换命令中,可以使用反斜杠引用前面的正则表达式中捕获的分组。例如,如果在正则表达式中使用了括号捕获了一个子字符串,可以在替换命令中使用 \1 引用该子字符串。
[root@halou-gf lianxi]echo aaafdfd bbb ccc |sed -nr 's/([a-z]+) ([a+z]+) ([a-z]+)/\3 \2 \1/p'
上边的那个命令 不就是 引用第三个 第二个 第一个子字符串嘛
+表示 出现1次以上
? 0-1次
*表示任意字符
| 或的意思
& 引用之前匹配的内容,前面查找匹配的内容
练习
新建一个cs.txt,内容如下:
hehaotian luqianjun zhangyi lianyu
hehaotian luqianjun lipengfei zhangyi wuhao
hehaotian lipengfei luqianjun zhangyi lianyu
sanchuang 34567 feng
changsha@12345 sanchuang
1.删除zhangyi
[root@gaohui try-test]sed -i.bak 's/zhangyi//g' sc.txt
[root@gaohui try-test]cat sc.txt
hehaotian luqianjun lianyu
hehaotian luqianjun lipengfei wuhao
hehaotian lipengfei luqianjun lianyu
sanchuang 34567 feng
changsha@12345 sanchuang
2.删除每行里的数字,或者特殊符号
[root@gaohui try-test]sed -i.bak -r 's/([0-9]|[^a-Z ])//g' sc.txt
[root@gaohui try-test]cat sc.txt
hehaotian luqianjun lianyu
hehaotian luqianjun lipengfei wuhao
hehaotian lipengfei luqianjun lianyu
sanchuang feng
changsha sanchuang
[root@gaohui try-test]#
3.将3行注释,前面加一个#号
[root@gaohui try-test]sed -i.bak -r '3 s/^/#/' sc.txt
[root@gaohui try-test]cat sc.txt
hehaotian luqianjun lianyu
hehaotian luqianjun lipengfei wuhao
#hehaotian lipengfei luqianjun lianyu
sanchuang feng
changsha sanchuang
4.包含lipengfei的行前面加注释
[root@gaohui try-test]# sed -i.bak '/lipengfei/ s/^/#/' sc.txt
[root@gaohui try-test]# cat sc.txt
hehaotian luqianjun lianyu
#hehaotian luqianjun lipengfei wuhao
##hehaotian lipengfei luqianjun lianyu
sanchuang feng
changsha sanchuang
5.在luqianjun字符串后面加一个sanchuang
[root@gaohui try-test]sed -i.bak 's/luqianjun/&sanchuang/g' sc.txt
[root@gaohui try-test]cat sc.txt
hehaotian luqianjunsanchuang lianyu
#hehaotian luqianjunsanchuang lipengfei wuhao
##hehaotian lipengfei luqianjunsanchuang lianyu
sanchuang feng
changsha sanchuang
&符号表示将匹配到的字符串替换为自身
6.以wuhao结尾的行,在末尾加一个zhengyani
[root@gaohui try-test]sed -i.bak 's/wuhao/&zhengyani/g' sc.txt
[root@gaohui try-test]cat sc.txt
hehaotian luqianjunsanchuang lianyu
#hehaotian luqianjunsanchuang lipengfei wuhaozhengyani
##hehaotian lipengfei luqianjunsanchuang lianyu
sanchuang feng
changsha sanchuang
[root@gaohui try-test]#
7./etc/hostname 文件里的名字修改为gaohui
[root@gaohui try-test]# sed -i 'c gaohui' /etc/hostname
[root@gaohui try-test]# cat /etc/hostname
gaohui
8.复制/etc/hosts文件到当前目录下,然后进行操作在每行前面加一个字符串sanchuang
[root@gaohui try-test]# sed -i.bak 's/^/sanchuang/g' hosts
[root@gaohui try-test]# cat hosts
sanchuang127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
sanchuang::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@gaohui try-test]#
9.自己编辑一个文件test.txt,内容如下:
0.0.0.0
1.1.1.1
2.2.2.2
使用sed或者awk或者编写脚本(shell,python,go等)实现输出以下形式:
0.0.0.0:80,1.1.1.1:80,2.2.2.2:80
[root@gaohui try-test]sed -i.bak 'N;N;s/\n/:80,/g;s/$/:80/' test.txt
[root@gaohui try-test]cat test.txt
0.0.0.0:80,1.1.1.1:80,2.2.2.2:80
[root@gaohui try-test]#
N表示取出下一行,两个N是两行,把换行符换成 :80, 最后一行没有换行符,就给他单独加个:80
10.新建一个文本文件sc.txt,内容如下:
abcdSdddde
islHishbxld
goBkefji
daanshXxge
使用sed去处理sc.txt给含有大写字母的行,在大写字母后追加数字2023
[root@halou-gf test2]sed -i.haha 's/[A-Z]/&2023/' sc.txt
[root@halou-gf test2]cat sc.txt
abcdS2023dddde
islH2023ishbxld
goB2023kefji
daanshX2023xge
[root@halou-gf test2]#
11.复制/etc/passwd文件到当前目录下,对当前目录下的passwd文件进行操作 --》请不要直接对/etc/passwd操作
请将所有的非root用户的uid后面加一个0,gid前面加一个1
[root@gaohui try-test]sed -i -r '/^root/! s/^(.*:.*:)([0-9]+):([0-9]+)/\1\20:1\3/' passwd
12.复制/etc/ssh/sshd_config到当前目录下,修改里面的端口号修改为8899
[root@halou-gf test2]sed -i.bak '/Port/ c Port 8899' sshd_config.bak