1. 概念说明
字符流编辑工具(行编辑工具) ==> 按照每行中的字符进行处理操作
全屏编辑工具 vi/vim
2. 作用说明
擅长对行进行操作处理
擅长将文件的内容信息进行修改,调整,删除
编写脚本常用
2.1 具体功能作用
向文件中添加信息(增)
删除文件中的信息(删)
修改文件中的信息(改)
过滤文件中的信息(查)
3. sed 命令的语法信息
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
命令 参数 条件+处理=(指令) 处理文件信息
# 显示出文件中有 yunxuan 行的信息
sed -n '/yunxuan/p' /data/web.conf
3.1 sed命令的指令信息
p 输出信息
i 插入信息,在指定信息前面插入新的信息
a 追加信息,在指定信息后面追加新的信息
d 删除指定信息
s 替换信息
g 全局替换(不加g 只替换一行中出现的第一个目标信息)
c 替换修改指定的一整行信息
3.2 sed 命令的参数信息
-n 取消默认输出
-r 识别扩展正则
-i 真实编辑文件(将内存中的信息覆盖到磁盘中)
-e 可以识别 sed 命令多个操作指令
4. sed 命令执行原理
5. sed命令实践操作
5.1 查询
- 环境准备
cat > test.txt <<EOF
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
EOF
5.1.1 根据文件内容的行号进行查询
- 显示单行信息
# 显示 test.txt 文件中第3行信息
sed -n '3p' test.txt
[root@templates tmp]# sed -n '3p' test.txt
103,zhangsan,COO
- 根据行号信息,连续输出多行信息
# 显示 test.txt 文件中第1-3行信息
sed -n '1,3p' test.txt
[root@templates tmp]# sed -n '1,3p' test.txt
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
- 根据行号信息,输出多行信息(不连续)
# 显示 test.txt 文件中 第1行 和 第3行 信息
sed -n '1p;3p' test.txt
[root@templates tmp]# sed -n '1p;3p' test.txt
101,yunxuan,CEO
103,zhangsan,COO
5.1.2 根据文件内容的信息进行查询
- 根据内容信息,输出单行信息
# 将文件 test.txt 中有 "yunxuan" 行的信息找出来
sed -n '/yunxuan/p' test.txt
[root@templates tmp]# sed -n '/yunxuan/p' test.txt
101,yunxuan,CEO
- 根据内容信息,输出多行信息(连续)
# 将文件 test.txt 中 yunxuan 到 lisi 行的信息都输出
sed -n '/yunxuan/,/lisi/p' test.txt
[root@templates tmp]# sed -n '/yunxuan/,/lisi/p' test.txt
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
# 扩展-注意项(跟sed命令执行原理有关)
[root@templates tmp]# echo "106,yunxuan,CEOO" >> test.txt
[root@templates tmp]# sed -n '/yunxuan/,/lisi/p' test.txt
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
106,yunxuan,CEOO
- 根据内容信息,输出多行信息(不连续)
# 将文件 test.txt 中 yunxuan 和 lisi 行的信息输出
sed -n '/yunxuan/p;/lisi/p' test.txt
[root@templates tmp]# sed -n '/yunxuan/p;/lisi/p' test.txt
101,yunxuan,CEO
104,lisi,CFO
106,yunxuan,CEOO
5.2 添加
5.2.1 在文件的第一行添加信息
- 在第一行的前面添加内容
# 将文件 test.txt 中 第一行添加 100,xx,UFO 内容
sed 'i100,xx,UFO' test.txt
# 不指定行号,sed 每次读取一行内容时,都会添加
[root@templates tmp]# sed 'i100,xx,UFO' test.txt
100,xx,UFO
101,yunxuan,CEO
100,xx,UFO
102,yunxi,CTO
100,xx,UFO
103,zhangsan,COO
100,xx,UFO
104,lisi,CFO
100,xx,UFO
105,wangwu,CIO
100,xx,UFO
106,yunxuan,CEOO
sed '1i100,xx,UFO' test.txt
# 在内存中进行添加内容,如果想写到block中,加 -i 参数
[root@templates tmp]# sed '1i100,xx,UFO' test.txt
100,xx,UFO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
106,yunxuan,CEOO
[root@templates tmp]# cat test.txt
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
106,yunxuan,CEOO
# 加 -i 参数,将添加内容写到block中
sed -i '1i100,xx,UFO' test.txt
[root@templates tmp]# sed -i '1i100,xx,UFO' test.txt
[root@templates tmp]# cat test.txt
100,xx,UFO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
106,yunxuan,CEOO
5.2.2 在文件的最后一行添加信息
- 在最后一行的之后添加内容
# 指定结尾行号
sed -i '7a107,yy,UFO' test.txt
[root@templates tmp]# sed -i '7a107,yy,UFO' test.txt
[root@templates tmp]# cat test.txt
100,xx,UFO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
106,yunxuan,CEOO
107,yy,UFO
# 使用$符号
sed -i '$a107,yy,UFO' test.txt
[root@templates tmp]# sed -i '$a107,yy,UFO' test.txt
[root@templates tmp]# cat test.txt
100,xx,UFO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
106,yunxuan,CEOO
107,yy,UFO
- 在最后一行之前添加内容
# 指定结尾行号
sed -i '7i105,yy,UFO' test.txt
[root@templates tmp]# sed -i '7i105,yy,UFO' test.txt
[root@templates tmp]# cat test.txt
100,xx,UFO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
105,yy,UFO
106,yunxuan,CEOO
# 使用$符号
[root@templates tmp]# sed -i '$i105,yy,UFOO' test.txt
[root@templates tmp]# cat test.txt
100,xx,UFO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
105,yy,UFOO
106,yunxuan,CEOO
5.2.3 扩展内容
- 在某字符串所在行的前面添加 信息 ,同时在该行的后面也添加 信息
# 在 存在 yy 行的前面 添加 yunxuan ,在其后面添加 admin
sed -i -e '/yy/iyunxuan' -e '/yy/aadmin' test.txt
# 未加 -i 参数
[root@templates tmp]# sed -e '/yy/iyunxuan' -e '/yy/aadmin' test.txt
100,xx,UFO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
yunxuan
1055,yy,UFO
admin
106,yunxuan,CEOO
[root@templates tmp]# cat test.txt
100,xx,UFO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
1055,yy,UFO
106,yunxuan,CEOO
# 加 -i 参数
[root@templates tmp]# sed -i -e '/yy/iyunxuan' -e '/yy/aadmin' test.txt
[root@templates tmp]# cat test.txt
100,xx,UFO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
yunxuan
1055,yy,UFO
admin
106,yunxuan,CEOO
- 某行的后面添加多行内容
# 在 最后一行后 添加两行内容 分别为 110,qq,TEST 和 111,ww,TEST
sed -i '$a110,qq,TEST\n111,ww,TEST' test.txt
[root@templates tmp]# sed -i '$a110,qq,TEST\n111,ww,TEST' test.txt
[root@templates tmp]# cat test.txt
100,xx,UFO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
yunxuan
1055,yy,UFO
admin
106,yunxuan,CEOO
110,qq,TEST
111,ww,TEST
5.2.4 在文件中添加内容的方法
1. vim/vi
2. cat >> 文件名 <<EOF xxxx EOF
3. echo -e "xxxx\nxxxx" 文件名
4. sed -i 'na/ixxxxx\nxxxxxx' 文件名
5.3 删除
5.3.1 删除单行信息
- 根据行号删除单行信息
# 删除 test.txt 文件中 第 7 行信息
sed -i '7d' test.txt
[root@templates tmp]# cat test.txt
100,xx,UFO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
yunxuan
yunxuan
1055,yy,UFO
admin
admin
106,yunxuan,CEOO
110,qq,TEST
111,ww,TEST
[root@templates tmp]# sed -i '7d' test.txt
[root@templates tmp]# cat test.txt
100,xx,UFO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
yunxuan
1055,yy,UFO
admin
admin
106,yunxuan,CEOO
110,qq,TEST
111,ww,TEST
- 根据文件内容删除单行信息
# 删除文件中 带有 qq 一行的信息
sed -i '/qq/d' test.txt
[root@templates tmp]# cat test.txt
100,xx,UFO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
106,yunxuan,CEOO
110,qq,TEST
111,ww,TEST
[root@templates tmp]# sed -i '/qq/d' test.txt
[root@templates tmp]# cat test.txt
100,xx,UFO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
106,yunxuan,CEOO
111,ww,TEST
5.3.2 删除多行信息
- 根据行号删除多行信息
# 删除 test.txt 中 第7行 至 第10行 内容 (连续)
sed -i '7,10d' test.txt
[root@templates tmp]# cat test.txt
100,xx,UFO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
yunxuan
1055,yy,UFO
admin
admin
106,yunxuan,CEOO
110,qq,TEST
111,ww,TEST
[root@templates tmp]# sed -i '7,10d' test.txt
[root@templates tmp]# cat test.txt
100,xx,UFO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
106,yunxuan,CEOO
110,qq,TEST
111,ww,TEST
# 删除 第1行 和 第8行
sed -i '1d;8d' test.txt
[root@templates tmp]# cat test.txt
100,xx,UFO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
106,yunxuan,CEOO
111,ww,TEST
[root@templates tmp]# sed -i '1d;8d' test.txt
[root@templates tmp]# cat test.txt
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
106,yunxuan,CEOO
5.3.3 利用sed命令取消空行显示
- 方法一
[root@templates tmp]# cat yunxuan.txt
001
002
003
eof
eof
[root@templates tmp]# sed '/^$/d' yunxuan.txt
001
002
003
eof
eof
- 方法二
[root@templates tmp]# sed -n '/^$/!p' yunxuan.txt
001
002
003
eof
eof
- 方法三
[root@templates tmp]# sed -n '/./p' yunxuan.txt
001
002
003
eof
eof
5.4 修改(替换)
sed -i 's#原内容#修改后内容#g' 文件名
- 修改文件时同时备份
sed -i.bak 's#yunxuan#admin#g' test.txt
[root@templates tmp]# sed -i.bak 's#yunxuan#admin#g' test.txt
[root@templates tmp]# ll
-rw-r--r-- 1 root root 88 Aug 20 16:42 test.txt
-rw-r--r-- 1 root root 92 Aug 20 12:55 test.txt.bak
[root@templates tmp]# cat test.txt
101,admin,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
106,admin,CEOO
替换信息不允许参数n和i一起使用
[root@templates tmp]# cat test.txt
101,yunxuan,CEO
101,yunxuan,CEO
102,yunxi,CTO
103,zhangsan,COO
104,lisi,CFO
105,wangwu,CIO
106,yunxuan,CEOO
106,yunxuan,CEOO
# -ni 参数同时使用,会将文件内容进行清空
[root@templates tmp]# sed -ni 's#yunxuan#admin#gp' test.txt
[root@templates tmp]# cat test.txt
101,admin,CEO
101,admin,CEO
106,admin,CEOO
106,admin,CEOO
- 利用 sed 命令批量重命名文件名
# 方法一
[root@templates data]# ls -l|sed -rn '3,9s#.* y(.*)\.(.*)#mv y\1.\2 y\1.jpg#gp'
mv yunxuan04.txt yunxuan04.jpg
mv yunxuan05.txt yunxuan05.jpg
mv yunxuan06.txt yunxuan06.jpg
mv yunxuan07.txt yunxuan07.jpg
mv yunxuan08.txt yunxuan08.jpg
mv yunxuan09.txt yunxuan09.jpg
mv yunxuan10.txt yunxuan10.jpg
[root@templates data]# ls -l|sed -rn '3,9s#.* y(.*)\.(.*)#mv y\1.\2 y\1.jpg#gp'|bash
[root@templates data]# ll
total 0
drwxr-xr-x 2 root root 48 Aug 19 10:33 test
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan04.jpg
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan05.jpg
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan06.jpg
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan07.jpg
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan08.jpg
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan09.jpg
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan10.jpg
# 方法二 (& 符号将 命令中(s###g) 第一个和第二个#号之间的信息 直接拿过来)
[root@templates data]# ls yunxuan*.jpg|sed -r 's#(.*)jpg#mv & \1.txt#g'
mv yunxuan04.jpg yunxuan04..txt
mv yunxuan05.jpg yunxuan05..txt
mv yunxuan06.jpg yunxuan06..txt
mv yunxuan07.jpg yunxuan07..txt
mv yunxuan08.jpg yunxuan08..txt
mv yunxuan09.jpg yunxuan09..txt
mv yunxuan10.jpg yunxuan10..txt
[root@templates data]# ls yunxuan*.jpg|sed -r 's#(.*)jpg#mv & \1.txt#g'|bash
[root@templates data]# ll
total 0
drwxr-xr-x 2 root root 48 Aug 19 10:33 test
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan04..txt
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan05..txt
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan06..txt
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan07..txt
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan08..txt
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan09..txt
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan10..txt
- 批量重命名命令
rename .txt .jpg yunxuan*.txt
[root@templates data]# ll
total 0
drwxr-xr-x 2 root root 48 Aug 19 10:33 test
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan04..txt
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan05..txt
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan06..txt
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan07..txt
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan08..txt
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan09..txt
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan10..txt
[root@templates data]# rename .txt .jpg yunxuan*.txt
[root@templates data]# ll
total 0
drwxr-xr-x 2 root root 48 Aug 19 10:33 test
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan04..jpg
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan05..jpg
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan06..jpg
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan07..jpg
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan08..jpg
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan09..jpg
-rw-r--r-- 1 root root 0 Aug 19 10:30 yunxuan10..jpg
- 替换修改一整行信息
[root@templates tmp]# cat test.txt
101,admin,CEO
101,admin,CEO
106,admin,CEOO
106,admin,CEOO
[root@templates tmp]# sed -i '2cHello World' test.txt
[root@templates tmp]# cat test.txt
101,admin,CEO
Hello World
106,admin,CEOO
106,admin,CEOO
6. 利用 sed 命令取出 IP 地址
6.1 步骤及思路
取出有 IP 信息的行
取出 IP 地址
6.2 方法
- 方法一
ip address show eth0|sed -n '3p'|sed -r 's#^.*net (.*)#\1#g'|sed -r 's#(.*)/24.*#\1#g'
[root@templates ~]# ip address show eth0|sed -n '3p'
inet 10.28.61.5/24 brd 10.28.61.255 scope global noprefixroute eth0
[root@templates ~]# ip address show eth0|sed -n '3p'|sed -r 's#^.*net (.*)#\1#g'
10.28.61.5/24 brd 10.28.61.255 scope global noprefixroute eth0
[root@templates ~]# ip address show eth0|sed -n '3p'|sed -r 's#^.*net (.*)#\1#g'|sed -r 's#(.*)/24.*#\1#g'
10.28.61.5
# 整合升级
[root@templates ~]# ip address show eth0|sed -n '3p'|sed -r 's#^.*inet (.*)/24(.*)#\1#g'
10.28.61.5
# 整合升级
[root@templates ~]# ip address show eth0|sed -rn '3s#^.*inet (.*)/24.*#\1#gp'
10.28.61.5
7. 扩展-sed命令显示行号
[root@templates tmp]# sed '=' test.txt | xargs -n2
1 101,admin,CEO
2 HelloWorld
3 106,admin,CEOO
4 106,admin,CEOO