微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/wenti/3279599.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐