sed随笔

sed [-hnV]  [-e<script>][-f<script文件>]    [文本文件]


参数说明:

  -e<script>或--expression=<script>   以选项中指定的script来处理输入的文本文件。
  -f<script文件>或--file=<script文件>   以选项中指定的script文件来处理输入的文本文件。
  -h或--help               显示帮助。
  -n或--quiet或--silent         仅显示script处理后的结果。
  -V或--version            显示版本信息。
动作说明:

  a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
  c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
  d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
  i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
  p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
  s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!如 1,2s/old/new/g 

  w: 用来将文本中指定行的内容写入文件中,此命令的基本格式如: [address]w filename

  y: 转换命令是唯一可以处理单个字符的 sed 脚本命令,其基本格式如下:[address]y/inchars/outchars/

  q: 是使 sed 命令在第一次匹配任务结束后,退出 sed 程序

一.a用法示例:

1.第三行加你要的内容 

[root@localhost tmp]# cat data.txt
1
2
3
4
5

[root@localhost tmp]# sed '3a add word' data.txt
1
2
3
add word
4
5

2.末尾加

[root@www ~]# sed -i '$a # This is a test' regular_express.txt
[root@www ~]# cat regular_express.txt
runoob!
google!
taobao!
facebook!
zhihu-
weibo-
# This is a test

3.匹配关键字后再下一行加内容

[root@localhost tmp]# cat test.txt
chen
ling
shuiqing

[root@localhost tmp]# sed '/chen/a add me' test.txt
chen
add me
ling
shuiqing

二、c用法

1.替换第三行

[root@localhost tmp]# sed '3c change me' data.txt
1
2
change me
4
5

2.末尾替换

[root@localhost tmp]# sed -r '$c change me' data.txt
1
2
3
4
5
change me

3.匹配关键字替换整行
[root@localhost tmp]# sed -r '/4/c change me' data.txt
1
2
3
change me
5

三、d用法

1.删除第1,2行

[root@localhost tmp]# sed '1,2d' data.txt
3
4
5

2.匹配关键字删除

[root@localhost tmp]# sed '/3/d' data.txt
1
2
4
5

四、i用法

1.第二行上面加内容

[root@localhost tmp]# sed '2i add work' data.txt
1
add work
2
3
4
5

2.匹配关键字的上以上加内容

[root@localhost tmp]# sed '/3/i add work' data.txt
1
2
add work
3
4
5

五、p用法

1.匹配关键字打印出来,在遍历整个

[root@localhost tmp]# sed '/1/p' data.txt
1
1
2
3
4
5

2.匹配关键字打印出来,加-n屏蔽掉不是匹配的内容

[root@localhost tmp]# sed -n '/1/p' data.txt
1

六、s用法

sed 's/要被取代的字串/新的字串/g'  g本行全部替换 数字1只替换第一个匹配的,数字3之i替换第三个匹配的,一次类推

1.匹配关键字更改

[root@localhost tmp]# sed 's/1/new1/g' data.txt
new1
2
3
4
5

2.加上e同时操作及替换,如下,将第三行到末行删掉,同时将bash替换成blueshell

nl /etc/passwd | sed -e '3,$d' -e 's/bash/blueshell/'
1 root:x:0:0:root:/root:/bin/blueshell
2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh

七、w用法

1.将data6.txt文件的第1,2行写入 test.txt文件中

[root@localhost ~]# sed '1,2w test.txt' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
[root@localhost ~]# cat test.txt
This is line number 1.
This is line number 2.

2.匹配的关键字那行都写入你要的文件中,如下:

[root@localhost tmp]# cat test
<html>
<title>First Wed</title>
<body>
h1chenh1
h2lingh2
h3shuiqingh3
</body>
</html>

[root@localhost tmp]# sed -n '/shuiqing/w me.txt' test
[root@localhost tmp]# cat me.txt
h3shuiqingh3

八、y用法

1.单个值进行一对一的映射,如下面的1对应4

[root@localhost ~]# echo "This 1 is a test of 1 try." | sed 'y/123/456/'
This 4 is a test of 4 try.

九、r用法

1.将data12.txt文件内容插入data6.txt文件的第3行下面开始,如下:

[root@localhost ~]# cat data12.txt
This is an added line.
This is the second added line.
[root@localhost ~]# sed '3r data12.txt' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is an added line.
This is the second added line.
This is line number 4.

#################################################

示例:

egg1:示例匹配关键字加上需要的符号

[root@localhost tmp]# cat test
<html>
<title>First Wed</title>
<body>
h1chenh1
h2lingh2
h3shuiqingh3
</body>
</html>

[root@localhost tmp]# sed '/h[0-9]/{s//\<&\>/1;s//\<\/&\>/2}' test
<html>
<title>First Wed</title>
<body>
<h1>chen</h1>
<h2>ling</h2>
<h3>shuiqing</h3>
</body>
</html>

 

egg2:sed可以拼接命令处理:如下

ll | grep -Ev "75|76|78" | awk '{print $NF}' | sed -r 's#(.*)#rm -f \1#g' | bash

除了75,76,78文件外的其他文件过滤出来交给sed拼接好命令,最后交给bash处理批量删除

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晨灵_queen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值