sed 命令使用方法

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 命令执行原理

image-20210822221433170.png

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 根据文件内容的行号进行查询

  1. 显示单行信息
# 显示 test.txt 文件中第3行信息
sed -n '3p' test.txt

[root@templates tmp]# sed -n '3p' test.txt 
103,zhangsan,COO
  1. 根据行号信息,连续输出多行信息
# 显示 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
  1. 根据行号信息,输出多行信息(不连续)
# 显示 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 根据文件内容的信息进行查询

  1. 根据内容信息,输出单行信息
# 将文件 test.txt 中有 "yunxuan" 行的信息找出来
sed -n '/yunxuan/p' test.txt

[root@templates tmp]# sed -n '/yunxuan/p' test.txt
101,yunxuan,CEO
  1. 根据内容信息,输出多行信息(连续)
# 将文件 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
  1. 根据内容信息,输出多行信息(不连续)
# 将文件 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 在文件的第一行添加信息

  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 在文件的最后一行添加信息

  1. 在最后一行的之后添加内容
# 指定结尾行号
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
  1. 在最后一行之前添加内容
# 指定结尾行号
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 扩展内容

  1. 在某字符串所在行的前面添加 信息 ,同时在该行的后面也添加 信息
# 在 存在 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
  1. 某行的后面添加多行内容
# 在 最后一行后 添加两行内容 分别为 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 删除单行信息

  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
  1. 根据文件内容删除单行信息
# 删除文件中 带有 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 删除多行信息

  1. 根据行号删除多行信息
# 删除 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 步骤及思路

  1. 取出有 IP 信息的行

  2. 取出 IP 地址

6.2 方法

  1. 方法一
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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,402评论 6 499
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,377评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,483评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,165评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,176评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,146评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,032评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,896评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,311评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,536评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,696评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,413评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,008评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,815评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,698评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,592评论 2 353

推荐阅读更多精彩内容