linux命令-sed命令使用(4)

来源:互联网 发布:淘宝网手机版登录 编辑:程序博客网 时间:2024/06/05 07:26
sed 编辑器的 更强大的功能。,插入,追加 修改,转化
sed 的基本使用
sed命令行格式为:
sed [-nefri] 'command' 输入文本

sed 中 插入,添加,修改, 转化
i ac y
i 会插入 在数据流 的前面,
a 会 追加 在数据流的后面。
c 修改
y 转化

[root@myCentOS ~]# echo"aaaabbbcccc"|sed 'i\Test Line one.'
Test Line one.
aaaabbbcccc
[root@myCentOS ~]# echo "aaaabbbcccc"|sed 'a\Test Line one.'
aaaabbbcccc
Test Line one.

抛砖引玉一下,下面来看看 如何用sed 插入的。

sed '[address]command\
new line ' filename
可以 用数字来指定行。
[root@myCentOS shell]# cat data7
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.

This is an inserted line插入到第四行前面
[root@myCentOS shell]# sed '4i\
This is an inserted line.' data7
This is line number 1.
This is line number 2.
This is line number 3.
This is an inserted line.
This is line number 4.
[root@myCentOS shell]# sed '4i\This is an inserted line.' data7
This is line number 1.
This is line number 2.
This is line number 3.
This is an inserted line.
This is line number 4.
[root@myCentOS shell]#

插入到第二行的后面。
[root@myCentOS shell]# sed '2a\This is an inserted line.' data7
This is line number 1.
This is line number 2.
This is an inserted line.
This is line number 3.
This is line number 4.
[root@myCentOS shell]#

插入到尾行,只要用$ 代表 尾行就可以了。
[root@myCentOS shell]# sed '$a\
> this is the end. ' data7
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
this is the end.


那么 如何添加 多行文本呢 ?
[root@myCentOS shell]# sed '1a\
> aaaaaaaaaaaaaaa\
> bbbbbbbbbbbbbb\
> ccccccccccccc' data7
This is line number 1.
aaaaaaaaaaaaaaa
bbbbbbbbbbbbbb
ccccccccccccc
This is line number 2.
This is line number 3.
This is line number 4.


当然 sed 也可以进行 修改,修改 整行的文本内容。
命令其实 和插入,追加类似 此时用 c ---> change
[root@myCentOS shell]# sed '2c\
This is a changed line of text.' data7
This is line number 1.
This is a changed line of text.
This is line number 3.
This is line number 4.

当然 也可以用文本模式 来进行寻址。
[root@myCentOS shell]# sed '/number 3/c\
> This is a changed line of text. ' data7
This is line number 1.
This is line number 2.
This is a changed line of text.
This is line number 4.


当然你也可以使用 地址空间 进行寻址
[root@myCentOS shell]# sed '2,3c\
> This is a changed line of text. ' data7
This is line number 1.
This is a changed line of text.
This is line number 4.
此时 会把2到3行所有的内容 ,修改为一行This is a changed line of text.



sed 中的转换命令
[address]y/inchars/outchars/
转换命令会进行inchars 和outchars值的一对一映射。 inchars 的第一个字符 会被转化为 outchars 中的第一个字符,第二个字符会转化成 outchars的第二个字符。 这个映射会一直持续到处理玩制定字符。 如果inchars和outchars 的长度 不同,则sed编辑器会产生一条错误消息。这样是不能够 转化的。
[root@myCentOS shell]# cat data8
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 5.
This is line number 6.
This is line number 7.
This is line number 8.
This is line number 9.
[root@myCentOS shell]# sed 'y/1234567/ABCdxyz/' data8
This is line number A.
This is line number B.
This is line number C.
This is line number d.
This is line number x.
This is line number y.
This is line number z.
This is line number 8.
This is line number 9.

1 转换成A, 2 转换为B, 3转换为C,4转换为d,... 7 转换为z 。

当然这里 也是可以指定一个范围的。比如 指定1到3行,进行转化。
[root@myCentOS shell]# sed '1,3y/1234567/ABCdxyz/' data8
This is line number A.
This is line number B.
This is line number C.
This is line number 4.
This is line number 5.
This is line number 6.
This is line number 7.
This is line number 8.
This is line number 9.


总结: 本文 sed 最基本的用法,插入,追加,修改 等基本的用法。 其中a i c 都是用 \ 斜线, y 这个 是用 / y/inchars/outchars/ ,并且 注意inchars 和outchars 是一一对应的关系


培训PPT:培训Sed基础入门.pptx
链接:http://pan.baidu.com/s/1skKXNy9 密码: i7fi


分享快乐,留住感动。 2017年 10月 10日 星期二 19:43:15 CST --biaoge


原创粉丝点击