shell sed工具

sed介绍

sed是Stream Editor(流编辑器)的缩写,简称流编辑器;用来处理文件的

sed是一行一行读取文件内容并按照要求进行处理,把处理后的结果输出到屏幕。

首先sed读取文件中的一行内容,把其保存在一个临时缓存区中(也称为模式空间)

然后根据需求处理临时缓冲区中的行,完成后把该行发送到屏幕上,由于sed把每一行都存在临时缓冲区中,对这个副本进行编辑,所以不会直接修改原文件。sed主要用来自动编辑一个或多个文件;简化对文件的反复操作,对文件进行过滤和转换操作

sed使用方法介绍

sed常见的语法格式有两种,一种叫命令行模式,另一种叫脚本模式。

1. 命令行格式

㈠ 语法格式

sed  [options]  '处理动作' 文件名

常见处理动作

 

㈡ 举例说明

文件准备

# vi a.txt

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

298374837483

172.16.0.254

10.1.1.1

① 对文件进行增、删、改、查操作

语法:sed 选项 '定位+命令' 需要处理的文件

1)打印文件内容

[root@tom ~]# sed  -n 'p'  a.txt                    打印每一行

[root@tom ~]# sed  -n '1p'  a.txt                    打印第1行

[root@tom ~]# sed  -n '2p'  a.txt                    打印第2行

[root@tom ~]# sed  -n '1,5p'  a.txt                打印1到5行

[root@tom ~]# sed  -n '$p' a.txt                     打印最后1行

2)增加文件内容

i 地址定位的上面插入

a 下面插入

[root@tom ~]# sed '$a99999' a.txt                 文件最后一行下面增加内容

[root@tom ~]# sed 'a99999' a.txt                 文件每行下面增加内容

[root@tom ~]# sed '5a99999' a.txt                 文件第5行下面增加内容

[root@tom ~]# sed '$i99999' a.txt                 文件最后一行上一行增加内容

[root@tom ~]# sed 'i99999' a.txt                 文件每行上一行增加内容

[root@tom ~]# sed '6i99999' a.txt                 文件第6行上一行增加内容

[root@tom ~]# sed '/^bin/ihello'              以bin开头行的上一行插入内容

3)修改文件内容

c 替换指定的整行内容

[root@tom ~]# sed '5chello world' a.txt         替换文件第5行内容

[root@tom ~]# sed 'chello world' a.txt         替换文件所有内容

[root@tom ~]# sed '1,5chello world' a.txt     替换文件1到5行内容为hello world

[root@tom ~]# sed '/^bin/c888888' a.txt    替换以bin开头的行

4)删除文件内容

[root@tom ~]# sed '1d' a.txt                         删除文件第1行

[root@tom ~]# sed '1,5d' a.txt                     删除文件1到5行

[root@tom ~]# sed '$d' a.txt                        删除文件最后一行

② 对文件进行搜索替换操作

语法:sed 选项 's/搜索的内容/替换的内容/动作' 需要处理的文件   

其中,s表示search搜索;斜杠/表示分隔符,可以自己定义;动作一般是打印p和全局替换g

[root@tom ~]# sed -n 's/root/ROOT/p' a.txt

[root@tom ~]# sed -n 's/root/ROOT/gp' a.txt

[root@tom ~]# sed -n 's/^#//gp' a.txt

[root@tom ~]# sed -n '1,5s/^/#/p' a.txt         注释掉文件的1-5行内容

[root@tom ~]# sed -n 's/\/sbin\/nologin/xyz/gp' a.txt

[root@tom ~]# sed -n 's@/sbin/nologin@xyz@gp' a.txt -- 自定义分隔符为@

[root@tom ~]# sed -n '10s#/sbin/nologin#xyz#p' a.txt --自定义分隔符为#

注意:搜索替换中的分隔符可以自己指定

③ 其他命令

 

举例说明:

r    从文件中读取输入行

w    将所选的行写入文件

&   保存查找串以便在替换串中引用   \(\)

[root@tom ~]# sed '3r /etc/hosts' a.txt

[root@tom ~]# sed '$r /etc/hosts' a.txt

[root@tom ~]# sed  '1,5w 11.txt' 1.txt

[root@tom ~]# sed -n 's/^sync/#&/gp' 1.txt
[root@tom ~]# sed -n 's/\(^sync\)/#\1/gp' 1.txt

=     打印行号

# sed -ne '/root/p' -ne '/root/=' 1.txt

# sed -n '/root/p;/root/=' 1.txt

!    对所选行以外的所有行应用命令,放到行数之后

[root@tom ~]# sed -n '1!p' 1.txt

[root@tom ~]# sed -n '4p' 1.txt

[root@tom ~]# sed -n '4!p' 1.txt

[root@tom ~]# cat -n 1.txt

[root@tom ~]# sed -n '1,17p' 1.txt

[root@tom ~]# sed -n '1,17!p' 1.txt

q    退出

# sed '5q' 1.txt

④ 其他选项

-e 多项编辑

-r 扩展正则

-i 修改原文件

[root@tom ~]# sed -ne '/root/p' 1.txt -ne '/root/='

root:x:0:0:root:/root:/bin/bash

1

[root@tom ~]# sed -ne '/root/=' -ne '/root/p' 1.txt

1

root:x:0:0:root:/root:/bin/bash

在1.txt文件中的第5行的前面插入“hello world”;在1.txt文件的第8行下面插入“哈哈哈哈”

[root@tom ~]# sed -e '5ihello world' -e '8a哈哈哈哈哈' 1.txt  -e '5=;8='

[root@tom ~]# sed -n '1,5p' 1.txt

[root@tom ~]# sed -ne '1p' -ne '5p' 1.txt

[root@tom ~]# sed -ne '1p;5p' 1.txt

[root@tom ~]# sed -nr '/([0-9]{1,3}\.){3}[0-9]{1,3}/p' 2.txt

-i 选项  直接修改原文件

# sed -i 's/root/ROOT/g' a.txt

⑤ sed结合正则使用

sed 选项 'sed命令或者正则表达式或者地址定位' 文件名

定址用于决定对哪些行进行编辑。地址的形式可以是数字、正则表达式、或二者的结合。

如果没有指定地址,sed将处理输入文件的所有行。

 

2. 脚本格式

脚本的第一行写上

#!/bin/sed -f

1,5d

s/root/hello/g

3i777

5i888

a999

p

㈠ 用法

# sed -f scripts.sh  file        //使用脚本处理文件

# ./sed.sh   file

sed -f 1.sed -i 1.txt

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一名初学袁

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

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

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

打赏作者

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

抵扣说明:

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

余额充值