linux-sed-案例100例

sed案例100例

文件a.txt内容:

I ask the indu1gence of the chi1dren who may read this book for dedicating it to a grown-up.
I have a serious reason:he is the best friend I have in the wor1d.
I have another reason:this grown-up understands everything,even books about chi1dren. 
I have a third reason:he 1ives in France where he is hungry and co1d.He needs cheering up.
If a11 these reasons are not enough,I wi11 dedicate the book to the chi1d from whom this grown-up grew.
A11 grown-ups were once chi1dren--a1though few of them remember it.And so I correct my dedication

1.删除文件每行的第一个字符

1.针对a.txt文件进行处理
2.执行语句:

[root@hdp0 sed]# sed  's/^.//g' a.txt   # 表示显示终端输出的 a.txt的内容中将每一行第一个字符删除,但不改变a.txt文件。
[root@hdp0 sed]# sed -i 's/^.//g' a.txt # cat a.txt 会发现文件中每行的首字母都被删掉了。
[root@hdp0 sed]# sed -r 's/(.)(.*)/\2/' b.txt # 表示将每一行的字符串分成两组,一组是第一个字符,另外一组是第一个字符后面的所有字符。\2:表示取第二组中的数据,变相的相当于删除了第一组的数据。
# 

3.备注: 上述指令使用了正则表达式。其中
特殊符号"^" 表示以某个字符开头;
特殊符号"." 可以表示任意的一个字符;
特殊符号( 和 ) 在替换命令中是“组”的用法(正则表达式中也是这样),后面可以用\1 \2 等来分别引用前面括起来的部分。
4.执行后显示内容:

 ask the indu1gence of the chi1dren who may read this book for dedicating it to a grown-up.
 have a serious reason:he is the best friend I have in the wor1d.
 have another reason:this grown-up understands everything,even books about chi1dren. 
 have a third reason:he 1ives in France where he is hungry and co1d.He needs cheering up.
f a11 these reasons are not enough,I wi11 dedicate the book to the chi1d from whom this grown-up grew.
11 grown-ups were once chi1dren--a1though few of them remember it.And so I correct my dedication

2.删除文件每行的第三个字符

[root@hdp0 sed]# sed ‘s/.//3’ a.txt # 将每一行中第三个字符置换成空,变相的删除了第三个字符。

显示结果:

I sk the indu1gence of the chi1dren who may read this book for dedicating it to a grown-up.
I ave a serious reason:he is the best friend I have in the wor1d.
I ave another reason:this grown-up understands everything,even books about chi1dren. 
I ave a third reason:he 1ives in France where he is hungry and co1d.He needs cheering up.
Ifa11 these reasons are not enough,I wi11 dedicate the book to the chi1d from whom this grown-up grew.
A1 grown-ups were once chi1dren--a1though few of them remember it.And so I correct my dedication

3.替换文本中的字符串

003.txt文件内容:

apple apple apple 
banana banana banana
fruit fruit fruit
book book book
book book book

结果:只是将每一行中第一个出现的book替换成books

[root@hdp0 sed]# sed 's/book/books/' 003.txt
apple apple apple 
banana banana banana
fruit fruit fruit
books book book
books book book

结果:将整个文件中所有的book替换成books

[root@hdp0 sed]# sed 's/book/books/g' 003.txt
apple apple apple 
banana banana banana
fruit fruit fruit
books books books
books books books

结果输出:-n选项和p命令一起使用表示只打印那些发生替换的行

[root@hdp0 sed]# sed -n 's/book/books/p' 003.txt 
books book book
books book book

结果:直接编辑文件选项-i,会匹配file文件中每一行的第一个book替换为good

[root@hdp0 sed]# sed -i 's/book/good/g' 003.txt
[root@hdp0 sed]# cat 003.txt 
apple apple apple 
banana banana banana
fruit fruit fruit
good good good
good good good

全面替换标记g

当需要从一行中的第N处匹配开始替换时,可以使用 /Ng:
案例1:从一行中的good出现的第二次开始后面所有的都将good替换成goods。

[root@hdp0 sed]# sed 's/good/goods/2g' 003.txt  
apple apple apple 
banana banana banana
fruit fruit fruit
good goods goods
good goods goods

4. 定界符 命令中字符 / 在sed中作为定界符使用,也可以使用任意的定界符

sed 's:test:TEXT:g' a.txt
sed 's|test|TEXT|g' a.txt

案例演示:

[root@hdp0 sed]# sed 's|good|book|g' 003.txt 
apple apple apple 
banana banana banana
fruit fruit fruit
book book book
book book book

定界符出现在样式内部时,需要进行转义:

 sed 's/\/bin/\/usr\/local\/bin/g' pwd.txt

5.删除操作:d命令

^:表示以某个字符开头。
KaTeX parse error: Expected group after '^' at position 13: :表示以某个字符结尾。 ^̲:表示将文件中为空的行筛选出来。

删除文件005.txt中所有的空行,并将文件内容进行输出。

[root@hdp0 sed]# sed '/^$/d' 005.txt 

删除第1行的数据,并再显示界面进行输出。

[root@hdp0 sed]# sed '1d' 005.txt  

删除第2,3行的数据,并再显示界面进行输出。

[root@hdp0 sed]# sed '2,3d' 005.txt 

删除第3行(包含第三行)之后的所有的数据,并再显示界面进行输出。

[root@hdp0 sed]# sed '3,$d' 005.txt  

删除最后一行的数据,并再显示界面进行输出。

[root@hdp0 sed]# sed '$d' 005.txt 

6.选定行的范围:,(逗号)

打印输出第一个出现fruit和第一个出现aaa中间的所有行数据(包含fruit和aaa所在的行)。

[root@hdp0 sed]# sed -n '/fruit/,/aaa/p' 005.txt  

打印从第5行开始到第一个包含以aaa开始的行之间的所有行:

[root@hdp0 sed]# sed -n '5,/aaa/p' 005.txt 
good good good
good good good

aaa aaa aaa aaa

对于模板第五行和mmm之间的行,每行的末尾用字符串boss替换:

[root@hdp0 sed]# sed  '5,/mmm/s/$/bosses/' 005.txt 

输出文件中第一行道第三行的数据。

 sed -n  '1,3p' 005.txt

7.已匹配字符串标记&

正则表达式 \w+ 匹配每一个单词,使用 [&] 替换它,& 对应于之前所匹配到的单词:

[root@hdp0 sed]# echo 'this is a test line' | sed 's/\w\+/[&]/g'
[this] [is] [a] [test] [line]

8.子串匹配标记\1

匹配给定样式的其中一部分:

[root@hdp0 sed]# echo this is digit 7 in a number | sed 's/digit \([0-9]\)/\1/'
this is 7 in a number

命令中 digit 7,被替换成了 7。样式匹配到的子串是 7,(…) 用于匹配子串,对于匹配到的第一个子串就标记为 \1,依此类推匹配到的第二个结果就是 \2,例如:

[root@hdp0 sed]# echo aaa BBB | sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/'   
BBB aaa

9. 引用

sed表达式可以使用单引号来引用,但是如果表达式内部包含变量字符串,就需要使用双引号。
案例:

[root@hdp0 sed]# test=hello
[root@hdp0 sed]#   echo hello world | sed "s/$test/hi~/"
hi~ world

10.多点编辑:e命令 -e选项允许在同一行里执行多条命令

删除文件中2到5行的数据,并将剩余内容中中字符a替换成#

[root@hdp0 sed]# sed -e '2,5d' -e 's/a/#/g' 005.txt 

上面sed表达式的第一条命令删除1至5行,第二条命令用#替换a。命令的执行顺序对结果有影响。如果两个命令都是替换命令,那么第一个替换命令将影响第二个替换命令的结果。

11.从文件读入:r命令

010.txt 的内容被读进来,显示在与test匹配的006.txt文件中的行后面,如果匹配多行,则010.txt 的内容将显示在所有匹配行的下面:

[root@hdp0 sed]# sed '/test/r 010.txt' 006.txt 
test test test test 
test test test test 
aaaa aaaa aaaa aaaa
bbbb bbbb bbbb bbbb

12.写入文件:w命令

在010.txt 中所有包含test的行都被写入011.txt里:

[root@hdp0 sed]# sed '/test/w 011.txt' 010.txt 
test test test test 
bbbb bbbb bbbb bbbb
test aaaa bbbb cccc
test cccc ccas dafa
asdd test aaaa bbbb
[root@hdp0 sed]# cat 011.txt 
test test test test 
test aaaa bbbb cccc
test cccc ccas dafa
asdd test aaaa bbbb

13.追加(行下):a\命令

将 this is a test line 追加到 以test 开头的行后面:

[root@hdp0 sed]# sed '/^test/a\this is a test line' 011.txt 
test test test test 
this is a test line
test aaaa bbbb cccc
this is a test line
test cccc ccas dafa
this is a test line
asdd test aaaa bbbb

14.插入(行上):i\

i\命令 将 this is a test line 追加到以test开头的行前面:

[root@hdp0 sed]# sed '1i\this is a test line' 011.txt 
this is a test line
test test test test 
test aaaa bbbb cccc

15.下一个:n命令

如果test被匹配,则移动到匹配行的下一行,替换这一行的aaaa,变为ssss,并打印该行,然后继续:

[root@hdp0 sed]# cat  012.txt 
test aaaa bbbb
aaaa bbbb cccc
test aaaa bbbb
aaaa aaaa aaaa
aaaa aaaa aaaa
aaaa aaaa aaaa
bbbb bbbb bbbb
test aaaa aaaa
bbbb bbbb bbbb
cccc aaaa aaaa
[root@hdp0 sed]#  sed '/test/{n; s/aaaa/ssss/;}' 012.txt 
test aaaa bbbb
ssss bbbb cccc
test aaaa bbbb
ssss aaaa aaaa
aaaa aaaa aaaa
aaaa aaaa aaaa
bbbb bbbb bbbb
test aaaa aaaa
bbbb bbbb bbbb
cccc aaaa aaaa

16.变形:y命令

把1~5行内所有abcd转变为大写,注意,正则表达式元字符不能使用这个命令:

[root@hdp0 sed]# sed '1,5 y/abcd/ABCD/' 013.txt   
ABCD ABCD AABB
ABCA ABCD ABBC
smno pqrs tuvw

17.保持和互换:h命令和x命令

互换模式空间和保持缓冲区的内容。也就是把包含test与check的行互换:
将用包含test的字符串的行来替换包含check字符串的行。

[root@hdp0 sed]# sed -e '/test/h' -e '/check/x' 014.txt 
test 001 
test 001 
test 003
test 003
[root@hdp0 sed]# cat 014.txt 
test 001 
check 002
test 003
check 004

18.打印奇数行或偶数行

[root@hdp0 sed]# cat 014.txt 
test 001 
check 002
test 003
check 004
[root@hdp0 sed]# sed -n 'p;n' 014.txt  # 打印奇数行
test 001 
test 003
[root@hdp0 sed]# sed -n 'n;p' 014.txt # 打印偶数行
check 002
check 004

19.打印匹配字符串的下一行

[root@hdp0 sed]# cat 014.txt 
test 001 
check 002
test 003
check 004
[root@hdp0 sed]# grep -A 1 001 014.txt 
test 001 
check 002
[root@hdp0 sed]# grep -A 1 001 014.txt -n 
1:test 001 
2-check 002
[root@hdp0 sed]# sed -n '/001/{n;p}' 014.txt 
check 002
[root@hdp0 sed]# awk '/001/{getline;print}' 014.txt 
check 002

20.使用sed命令打印出ifconfig ens33的ip地址

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.200  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::a68a:8e77:9c98:33e3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4a:12:3a  txqueuelen 1000  (Ethernet)
[root@hdp0 sed]# ifconfig ens33 | sed -r -n '2s/inet[[:space:]](.*)netmask.*/\1/p' 
        192.168.1.200 
[root@hdp0 sed]# ifconfig ens33 | sed -n '2 s/inet//p' |sed -r 's/(.*)netmask.*/\1/' 
         192.168.1.200      
[root@hdp0 sed]# ifconfig ens33 | sed -n '2p' | sed 's/.*inet//' | sed 's/netmask.*//'
 192.168.1.200    
[root@hdp0 sed]# ifconfig ens33 | sed -r -n '2s/.*inet(.*)netmask.*/\1/p'
 192.168.1.200   

21.使用sed命令打印出系统版本

[root@hdp0 ~]# cat  /etc/centos-release
CentOS Linux release 7.4.1708 (Core) 
[root@hdp0 ~]# cat /etc/centos-release | sed -r -n 's/.*.release[[:space:]](.*)[[:space:]]\(Core\)/\1/p'
7.4.1708 

22.三级目录

23.三级目录

24.三级目录

25.三级目录

26.三级目录

27.三级目录

28.三级目录

29.三级目录

30.三级目录

31.三级目录

32.三级目录

33.三级目录

34.三级目录

35.三级目录

36.三级目录

37.三级目录

38.三级目录

39.三级目录

40.三级目录

41.三级目录

42.三级目录

43.三级目录

44.三级目录

45.三级目录

46.三级目录

47.三级目录

48.三级目录

49.三级目录

50.三级目录

51.三级目录

52.三级目录

53.三级目录

54.三级目录

55.三级目录

56.三级目录

57.三级目录

58.三级目录

59.三级目录

60.三级目录

61.三级目录

62.三级目录

63.三级目录

64.三级目录

65.三级目录

66.三级目录

67.三级目录

68.三级目录

69.三级目录

70.三级目录

71.三级目录

72.三级目录

73.三级目录

74.三级目录

75.三级目录

76.三级目录

77.三级目录

78.三级目录

79.三级目录

80.三级目录

81.三级目录

82.三级目录

83.三级目录

84.三级目录

85.三级目录

86.三级目录

87.三级目录

88.三级目录

89.三级目录

90.三级目录

91.三级目录

92.三级目录

93.三级目录

94.三级目录

95.三级目录

96.三级目录

97.三级目录

98.三级目录

99.三级目录

100.三级目录

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值