shell文本处理三剑客之——sed编辑器

一、sed编辑器的概述

1、sed编辑器的概念

sed是一种流编辑器,流编辑器会在编辑器处理数据之前基于预先提供的一组规则来编辑数据流。
sed编辑器可以根据命令来处理数据流中的数据,这些命令要么从命令行中输入,要么存储在一个命令文本文件中。

2、sed编辑器的工作流程

sed 的工作流程主要包括读取、执行和显示三个过程∶

  • 读取∶ sed 从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓冲区中 (又称模式空间,pattern space)
  • 执行:默认情况下,所有的sed 命令都在模式空间中顺序地执行,除非指定了行的地址, 否则sed 命令将会在所有的行上依次执行。
  • 显示∶发送修改后的内容到输出流。在发送数据后, 模式空间将会被清空。在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。

在所有的文件内容都被处理完成之前,上述过程将重复执行, 直至所有内容被处理完。
注意∶ 默认情况下所有的sed命令都是在模式空间内执行的,因此输入的文件并不会发生任何变化,除非是用重定向存储输出。

3、sed命令的格式

第一种:
sed -e '操作' 文件1 文件2

第二种:
sed -n -e '操作' 文件1 文件2 

第三种:
sed -f 脚本文件 文件1 文件2 

第四种:
sed -i -e '操作' 文件1 文件2

4、sed命令的常用选项

-e 或 - -expression=∶ 表示用指定命令来处理输入的文本文件,只有一个操作命令时可省略,一般在执行多个操作命令使用
-f 或- -file=∶表示用指定的脚本文件来处理输入的文本文件。
-h 或- -help∶显示帮助。
-n、- -quiet 或 silent∶ 禁止sed编辑器输出,但可以与p命令一起使用完成输出。
-i∶ 直接修改目标文本文件。

5、sed命令的常用操作

s∶替换,替换指定字符。
d∶删除,删除选定的行。
a∶ 增加,在当前行下面增加一行指定内容。
i∶ 插入,在选定行上面插入一行指定内容。
c∶ 替换,将选定行替换为指定内容。
y∶ 字符转换,转换前后的字符长度必须相同。
p∶ 打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容; 如果有非打印字符,则以 AscII码输出。其通常与_n"选项一起使用。
=∶打印行号
l(小写L)∶打印数据流中的文本和不可打印的ASCII字符(比如结束符$、制表符\t)

r:是读命令。sed使用该命令将一个文本文件中的内容加到当前文件的特定位置上。

二、sed对文本的操作方式

1、打印内容

1.1  打印文件内容,不指定行  

[root@weq ~]# cat users.txt 
weq
xx
ly
lucy
nancy
[root@weq ~]# sed -n -e 'p' users.txt 
weq
xx
ly
lucy
nancy
[root@weq ~]# sed -n 'p' users.txt 
weq
xx
ly
lucy
nancy
[root@weq ~]# sed  'p' users.txt 
weq
weq
xx
xx
ly
ly
lucy
lucy
nancy
nancy

  

1.2  打印某行文件内容,指定行号

[root@weq ~]# cat users.txt 
weq
xx
ly
lucy
nancy
[root@weq ~]# sed -n '1p' users.txt 
weq

  

1.3 打印文件内容的行号  = 

[root@weq ~]# cat users.txt 
weq
xx
ly
lucy
nancy
[root@weq ~]# sed '=' users.txt 
1
weq
2
xx
3
ly
4
lucy
5
nancy
[root@weq ~]# sed -n '=' users.txt 
1
2
3
4
5

  

1.4   l(小写L)∶打印数据流中的文本和不可打印的ASCII字符(比如结束符$、制表符\t)

[root@weq ~]# cat users.txt 
weq
xx
ly
lucy
nancy
[root@weq ~]# sed -n 'l' users.txt 
weq$
xx$
ly$
lucy$
nancy$

  

1.5 打印文件内容行号和对应行号文件内容的四种方法:

[root@weq ~]# cat users.txt 
weq
xx
ly
lucy
nancy
[root@weq ~]# sed -n '=' users.txt 
1
2
3
4
5
[root@weq ~]# cat users.txt 
weq
xx
ly
lucy
nancy
[root@weq ~]# sed '=' users.txt 
1
weq
2
xx
3
ly
4
lucy
5
nancy
[root@weq ~]# sed -n '=;p' users.txt 
1
weq
2
xx
3
ly
4
lucy
5
nancy
[root@weq ~]# sed -n -e '=' -e 'p' users.txt 
1
weq
2
xx
3
ly
4
lucy
5
nancy
[root@weq ~]# sed -n '
> =
> p
> ' users.txt
1
weq
2
xx
3
ly
4
lucy
5
nancy  

2、 sed编辑器有2种寻址方式:

• 以数字形式表示行区间

• 用文本模式来过滤出行

2.1   以数字形式表示行区间一:打印指定的行  

[root@weq ~]# cat users.txt 
weq
xx
ly
lucy
nancy
[root@weq ~]# sed -n '1p' users.txt 
weq
[root@weq ~]# sed -n '$p' users.txt 
nancy
[root@weq ~]# sed -n '1,3p' users.txt 
weq
xx
ly
[root@weq ~]# sed -n '3,$p' users.txt 
ly
lucy
nancy
[root@weq ~]# sed -n -e '1p' -e '3p' users.txt 
weq
ly
[root@weq ~]# sed -n '1,+3p' users.txt 
weq
xx
ly
lucy
[root@weq ~]# sed '4q' users.txt 
weq
xx
ly
lucy

  

2.2  以数字形式表示行区间二:打印奇数行、偶数行  

[root@weq ~]# cat num.txt 
1
2
3
4
5
6
7
8
9
10
[root@weq ~]# sed -n 'n;p' num.txt
2
4
6
8
10
[root@weq ~]# sed -n 'p;n' num.txt 
1
3
5
7
9

  

2.3  以数字形式表示行区间三:打印间隔行(一行或者多行)

[root@weq ~]# cat num.txt 
1
2
3
4
5
6
7
8
9
10
[root@weq ~]# sed -n '3,${p;n}' num.txt 
3
5
7
9
[root@weq ~]# sed -n '3,${n;p}' num.txt 
4
6
8
10
[root@weq ~]# sed -n '2,${n;n;p}' num.txt 
4
7
10
[root@weq ~]# sed -n '2,${n;p;n}' num.txt 
3
6
9

  

2.4  用文本模式来过滤出行  一:过滤文本所在的行

[root@weq ~]# cat aaa.txt 
zhangsan1234@qq.com
lucy_789@sina.com.cn
li li@163.com
qian@qi@wo.cn
www 13@qq.com
[root@weq ~]# sed -n '/qq/p' aaa.txt 
zhangsan1234@qq.com
www 13@qq.com
[root@weq ~]# sed -n '/^l/p' aaa.txt 
lucy_789@sina.com.cn
li li@163.com
[root@weq ~]# sed -n '/cn$/p' aaa.txt 
lucy_789@sina.com.cn
qian@qi@wo.cn
[root@weq ~]# sed -n '/^l\|cn$/p' aaa.txt 
lucy_789@sina.com.cn
li li@163.com
qian@qi@wo.cn

  

2.5 用文本模式来过滤出行  二:-r表示支持扩展正则表达式

[root@weq ~]# sed -n '/^l\|cn$/p' aaa.txt 
lucy_789@sina.com.cn
li li@163.com
qian@qi@wo.cn
[root@weq ~]# sed -nr '/^l|cn$/p' aaa.txt 
lucy_789@sina.com.cn
li li@163.com
qian@qi@wo.cn

  

2.6  用文本模式来过滤出行  三:输出匹配的行,可结合区间过滤

[root@weq ~]# cat zz.txt 
lk
lok
look
loook
looook
loooook
looooook
looooooook
loooooaaak
labk
ooloooook
oolk
lobk
[root@weq ~]# sed -n '/lo\{2,4\}k/p' zz.txt 
look
loook
looook
[root@weq ~]# sed -nr '/lo{2,4}k/p' zz.txt 
look
loook
looook

  

2.7  用文本模式来过滤出行  四:过滤出从指定行开始到指定内容停止

[root@weq ~]# cat aaa.txt 
zhangsan1234@qq.com
lucy_789@sina.com.cn
li li@163.com
qian@qi@wo.cn
www 13@qq.com
li li@sina.com
[root@weq ~]# sed -n '2,/li/p' aaa.txt 
lucy_789@sina.com.cn
li li@163.com
[root@weq ~]# sed -n '2,/li/=' aaa.txt 
2
3
[root@weq ~]# sed -n -e '2,/li/=' -e '2,/li/p' aaa.txt 
2
lucy_789@sina.com.cn
3
li li@163.com

  

 3、删除内容

3.1  删除指定行的内容

[root@weq ~]# cat num.txt 
1
2
3
4
5
6
7
8
9
10
[root@weq ~]# sed 'd' num.txt 
[root@weq ~]# sed '2d' num.txt 
1
3
4
5
6
7
8
9
10
[root@weq ~]# sed '2,8d' num.txt 
1
9
10
[root@weq ~]# sed -e '2d' -e '8d' num.txt 
1
3
4
5
6
7
9
10
[root@weq ~]# sed '$d' num.txt 
1
2
3
4
5
6
7
8
9
[root@weq ~]# sed '/2/,/8/d' num.txt 
1
9
10
[root@weq ~]# cat testfile4
1111




2222
[root@weq ~]# sed '/^$/d' testfile4
1111
2222

  

3.1  删除包含指定内容的行

[root@weq ~]# cat aaa.txt 
zhangsan1234@qq.com
lucy_789@sina.com.cn
li li@163.com
qian@qi@wo.cn
www 13@qq.com
li li@sina.com
[root@weq ~]# sed '/li li/d' aaa.txt 
zhangsan1234@qq.com
lucy_789@sina.com.cn
qian@qi@wo.cn
www 13@qq.com
[root@weq ~]# sed '/li li/!d' aaa.txt 
li li@163.com
li li@sina.com

4、替换内容

4.1 格式

sed 行范围 s/旧字符串/新字符串/替换标记

4.2 四种替换标记:

  • 数字: 表明新字符串将替换第几处匹配的地方
  • g: 表明新字符串将会替换所有匹配的地方
  • p: 打印与替换命令匹配的行,与-n一起使用
  • w文件: 将替换的结果写到文件中

4.3、替换指定字符,并指定替换第几处或者替换所有(替换所有用g)

[root@weq ~]# cat 123.txt
zs:x:1028:1028::/home/zs:/bin/bash
xx:x:1003:1003::/home/xx:/bin/bash
users.txt:x:1001:1001::/home/users.txt:/bin/bash
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
stu9:x:1015:1015::/home/stu9:/bin/bash
stu8:x:1014:1014::/home/stu8:/bin/bash
stu7:x:1013:1013::/home/stu7:/bin/bash
[root@weq ~]# sed -n 's/stu/LY/p' 123.txt
LY9:x:1015:1015::/home/stu9:/bin/bash
LY8:x:1014:1014::/home/stu8:/bin/bash
LY7:x:1013:1013::/home/stu7:/bin/bash
[root@weq ~]# sed -n 's/stu/LY/2p' 123.txt
stu9:x:1015:1015::/home/LY9:/bin/bash
stu8:x:1014:1014::/home/LY8:/bin/bash
stu7:x:1013:1013::/home/LY7:/bin/bash
[root@weq ~]# sed -n 's/stu/LY/gp' 123.txt
LY9:x:1015:1015::/home/LY9:/bin/bash
LY8:x:1014:1014::/home/LY8:/bin/bash
LY7:x:1013:1013::/home/LY7:/bin/bash

 

4.4 替换空值,表示删除

[root@weq ~]# cat 123.txt
zs:x:1028:1028::/home/zs:/bin/bash
xx:x:1003:1003::/home/xx:/bin/bash
users.txt:x:1001:1001::/home/users.txt:/bin/bash
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
stu9:x:1015:1015::/home/stu9:/bin/bash
stu8:x:1014:1014::/home/stu8:/bin/bash
stu7:x:1013:1013::/home/stu7:/bin/bash
[root@weq ~]# sed -n 's/stu//gp' 123.txt
9:x:1015:1015::/home/9:/bin/bash
8:x:1014:1014::/home/8:/bin/bash
7:x:1013:1013::/home/7:/bin/bash
[root@weq ~]# sed -n 's/stu//1p' 123.txt
9:x:1015:1015::/home/stu9:/bin/bash
8:x:1014:1014::/home/stu8:/bin/bash
7:x:1013:1013::/home/stu7:/bin/bash

  

4.5 指定行范围进行替换,并将替换结果保存到指定文件中

[root@weq ~]# sed '=' 123.txt
1
zs:x:1028:1028::/home/zs:/bin/bash
2
xx:x:1003:1003::/home/xx:/bin/bash
3
users.txt:x:1001:1001::/home/users.txt:/bin/bash
4
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
5
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
6
tcpdump:x:72:72::/:/sbin/nologin
7
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
8
sync:x:5:0:sync:/sbin:/bin/sync
9
stu9:x:1015:1015::/home/stu9:/bin/bash
10
stu8:x:1014:1014::/home/stu8:/bin/bash
11
stu7:x:1013:1013::/home/stu7:/bin/bash
[root@weq ~]# sed '1,3s/home/LY/g' 123.txt > ly.txt
[root@weq ~]# cat ly.txt 
zs:x:1028:1028::/LY/zs:/bin/bash
xx:x:1003:1003::/LY/xx:/bin/bash
users.txt:x:1001:1001::/LY/users.txt:/bin/bash
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
stu9:x:1015:1015::/home/stu9:/bin/bash
stu8:x:1014:1014::/home/stu8:/bin/bash
stu7:x:1013:1013::/home/stu7:/bin/bash

  

4.6    sed 行范围 s/旧字符串/新字符串/替换标记 ,该格式中的/可用其他符号或者字母数字替代,只要保持一致即可,如#、?等等

当被替换的内容中包含/,则替换时会出现多个/,容易混淆,则可用其他符号或者字母数字替代,保持一致即可  

举例:用其他字符代替/

[root@weq ~]# cat 123.txt
zs:x:1028:1028::/home/zs:/bin/bash
xx:x:1003:1003::/home/xx:/bin/bash
users.txt:x:1001:1001::/home/users.txt:/bin/bash
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
stu9:x:1015:1015::/home/stu9:/bin/bash
stu8:x:1014:1014::/home/stu8:/bin/bash
stu7:x:1013:1013::/home/stu7:/bin/bash
[root@weq ~]# sed 's#/bin/bash#/Usr/sbin#g' 123.txt
zs:x:1028:1028::/home/zs:/Usr/sbin
xx:x:1003:1003::/home/xx:/Usr/sbin
users.txt:x:1001:1001::/home/users.txt:/Usr/sbin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
stu9:x:1015:1015::/home/stu9:/Usr/sbin
stu8:x:1014:1014::/home/stu8:/Usr/sbin
stu7:x:1013:1013::/home/stu7:/Usr/sbin
[root@weq ~]# sed 's?/bin/bash?/Usr/sbin?g' 123.txt
zs:x:1028:1028::/home/zs:/Usr/sbin
xx:x:1003:1003::/home/xx:/Usr/sbin
users.txt:x:1001:1001::/home/users.txt:/Usr/sbin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
stu9:x:1015:1015::/home/stu9:/Usr/sbin
stu8:x:1014:1014::/home/stu8:/Usr/sbin
stu7:x:1013:1013::/home/stu7:/Usr/sbin

  

==注意:==可以自定义分隔符进行分隔,主要看s 后面跟着什么字符,就代表以什么字符进行分隔

举例:用数字代替分隔符

[root@weq ~]# cat ly.txt 
8945668876438687898
[root@weq ~]# sed -n 's8\87\89\88\89\878p' ly.txt 
894566887643868987

  

4.7 行首或者行尾添加指定字符  

[root@weq ~]# cat aaa.txt 
zhangsan1234@qq.com
lucy_789@sina.com.cn
li li@163.com
qian@qi@wo.cn
www 13@qq.com
li li@sina.com
[root@weq ~]# sed '1,3 s/^/#/' aaa.txt 
#zhangsan1234@qq.com
#lucy_789@sina.com.cn
#li li@163.com
qian@qi@wo.cn
www 13@qq.com
li li@sina.com
[root@weq ~]# sed '1,3 s/$/!/' aaa.txt 
zhangsan1234@qq.com!
lucy_789@sina.com.cn!
li li@163.com!
qian@qi@wo.cn
www 13@qq.com
li li@sina.com

  

5、插入内容

sed '/45/c ABC' testfile2

sed '/45/ y/45/AB/' testfile2

sed '1,3a ABC' testfile2 

sed '1i ABC' testfile2

sed '5r /etc/resolv.conf' testfile2

sed '/root/{H;d};$G' /etc/passwd     #将包含root的行剪切到末尾,H表示复制到剪切板,G表示粘贴到指定行后

sed '1,2H;3,4G' /etc/passwd      #将1、2行复制到3和4行的下面

echo  "111222333" | sed 's/(111)(222)/2\1/' -r #将字符111和222互换位置
echo "111222333" | sed 's/^(.) (.*) (.)$/\3\2\1/' -r   #将第一个字符和最后- - 个字符互换:

5.1   c: 替换,将选定行替换为指定内容  

[root@weq ~]# cat testfile1
11
26
7
76
99
43
68
678
[root@weq ~]# sed '/7/c AAA' testfile1
11
26
AAA
AAA
99
43
68
AAA
[root@weq ~]# sed '2c BBB' testfile1 
11
BBB
7
76
99
43
68
678

    

 5.2  y: 字符转换,转换前后的字符长度必须相同

[root@weq ~]# cat testfile2
c
bd
abbc
cdd
efg
efg
abc
abc
CDD
[root@weq ~]# sed '/ab/ y/ab/89/' testfile2
c
bd
899c
cdd
efg
efg
89c
89c
CDD
[root@weq ~]# sed '/efg/ y/efg/56/' testfile2
sed:-e 表达式 #1,字符 15:“y”命令的字符串长度不同
[root@weq ~]# sed '/efg/ y/efg/567/' testfile2
c
bd
abbc
cdd
567
567
abc
abc
CDD

  

5.3   a: 增加,在当前行下面增加一行指定内容 

[root@weq ~]# cat testfile1
11
26
7
76
99
43
68
678
[root@weq ~]# sed '3,5a AAA' testfile1
11
26
7
AAA
76
AAA
99
AAA
43
68
678
[root@weq ~]# sed '$a AAA' testfile1
11
26
7
76
99
43
68
678
AAA

  

5.4  i: 插入,在选定行上面插入一行指定内容

[root@weq ~]# cat testfile1
11
26
7
76
99
43
68
678
[root@weq ~]# sed '3,5i AAA' testfile1
11
26
AAA
7
AAA
76
AAA
99
43
68
678
[root@weq ~]# sed '$i AAA' testfile1
11
26
7
76
99
43
68
AAA
678

  

5.5  r命令是读命令。sed使用该命令将一个文本文件中的内容加到当前文件的特定位置上

[root@weq ~]# cat testfile4
1111




2222
[root@weq ~]# cat testfile1
11
26
7
76
99
43
68
678
[root@weq ~]# sed '3r testfile4' testfile1
11
26
7
1111




2222
76
99
43
68
678

  

5.6 将输出的字符进行调整顺序

[root@weq ~]# echo aaabbbcccddd | sed -r 's/(aaa)(bbb)/\2\1/'
bbbaaacccddd
[root@weq ~]# echo aaabbbcccddd | sed -r 's/(aaa)(bbb)(ccc)/\3\2\1/'
cccbbbaaaddd

  

5.7 将输出的字符进行互换位置(第1个字符与第2个字符、第1个字符与最后1个字符)

每行文本拆分为“第1个字符(.)”、“第2个字符(.)”、“剩下的所有字符(.*)”三个部分,然后通过替换操作重排顺序为“2-1-3”:# sed -r 's/^(.)(.)(.*)/\2\1\3/' 123.txt

[root@weq ~]# echo 123 | sed -r 's/(.)(.)(.*)/\3\2\1/'
321
[root@weq ~]# echo 1a2b3c | sed -r 's/(.)(.)(.*)/\3\2\1/'
2b3ca1
[root@weq ~]# echo 123ABCabc | sed -r 's/(.)(.)(.)(.*)(.)(.)(.)/\7\6\5\4\3\2\1/'
cbaABC321

  

将第一个字符(.)与最后一字符(.)对调:sed -r “s/^(.)( .*)(.)$/\3\2\1/” 123.txt  中间的字符用(.*)表示

[root@weq ~]# echo 1a2b3c4d | sed -r 's/(.)(.*)(.)/\3\2\1/'
da2b3c41

  

三、剪切复制粘贴内容

1、模式空间与保持空间概念

  • 模式空间:存放当前处理的行,将处理结果输出

        若当前行不符合处理条件,则原样输出,处理完当前行再读入下一行来处理

  • 保持空间:作用类似于“剪贴板”默认存放一个空行(换行符\n)

2、剪切复制的基本动作(G、g、H、h)

复制:H:模式空间---[追加]--->保持空间

          h:模式空间---[覆盖]--->保持空间

粘贴:G:保持空间---[追加]--->模式空间

          g:保持空间---[覆盖]--->模式空间

剪切:复制结合d(删除)一起使用

3、实例操作

3.1  将指定的行的内容复制到指定的位置

[root@weq ~]# cat testfile2
c
bd
abbc
cdd
efg
abc
CDD
[root@weq ~]# sed '1,4H;$G' testfile2
c
bd
abbc
cdd
efg
abc
CDD

c
bd
abbc
cdd
[root@weq ~]# sed '1h;2,4H;$G' testfile2
c
bd
abbc
cdd
efg
abc
CDD
c
bd
abbc
cdd

  

3.2  将指定的内容剪切到指定的位置

[root@weq ~]# cat testfile2
c
bd
abbc
cdd
efg
abc
CDD
[root@weq ~]# sed '/c/{H;d};$G' testfile2
bd
efg
CDD

c
abbc
cdd
abc
[root@weq ~]# sed '1{H;d};$G' testfile2
bd
abbc
cdd
efg
abc
CDD

c
[root@weq ~]# sed '1{h;d};$G' testfile2
bd
abbc
cdd
efg
abc
CDD
c
[root@weq ~]# sed '2,5{H;d};$G' testfile2
c
abc
CDD

bd
abbc
cdd
efg
[root@weq ~]# sed '2{h;d};3,5{H;d};$G' testfile2
c
abc
CDD
bd
abbc
cdd
efg

  

posted on 2021-07-28 11:06  笑洋仟  阅读(200)  评论(0编辑  收藏  举报

levels of contents