shell脚本快速入门之-----正则三剑客之二sed用法大全!!!

时间:2022-07-24
本文章向大家介绍shell脚本快速入门之-----正则三剑客之二sed用法大全!!!,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、sed 工具简介

1、sed工具工作原理

  • sed(Stream EDitor)是一个强大而简单的文本解析转换工具,可以读取文本,并根据指定的条件对文本内容进行编辑(删除、替换、添加、移动等)
  • sed 工作的环境是在内存中更改写入文件 因为内存是断电就没了 那么怎么才能将写入的数据写到硬盘中 sed-i 覆盖一下 否则你改了也没用 工作原理图

sed 的工作流程主要包括读取、执行和显示三个过程。

  • 读取:sed 从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓冲区中(又称模式空间,pattern space)。
  • 执行:默认情况下,所有的 sed 命令都在模式空间中顺序地执行,除非指定了行的地址,否则 sed 命令将会在所有的行上依次执行。
  • 显示:发送修改后的内容到输出流。在发送数据后,模式空间将会被清空。 在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。

2、sed 命令选项主要包含以下几种

-e 或–expression=:表示用指定命令或者脚本来处理输入的文本文件。 -f 或–file=:表示用指定的脚本文件来处理输入的文本文件。 -h 或–help:显示帮助。 -n、–quiet 或 silent:表示仅显示处理后的结果。 -i:直接编辑文本文件

二、sed工具使用方法合集

1、输出符合条件的文本(p 表示正常输出)

[root@localhost ~]# sed -n 'p' test.txt//输出所有内容,等同于 cat test.txt
[root@localhost ~]# sed -n '3p' test.txt //输出第 3 行
[root@localhost ~]# sed -n '3,5p' test.txt //输出 3~5 行 

2、n表示不读取

[root@localhost ~]# sed -n 'p;n' test.txt //输出所有奇数行,n 表示读入下一行资料 也就是第一行读p正常输出  第二行之所以不显示 n直接读取下一行 就直接读第三行了
[root@localhost ~]# sed -n 'n;p' test.txt //输出所有偶数行,n 表示读入下一行资料
[root@localhost ~]# sed -n '1,5{p;n}' test.txt //输出第 1~5 行之间的奇数行(第 1、3、5 行)

3、读取指定行以后的要用$

这里的$符号

[root@localhost ~]# sed -n '10,${n;p}' test.txt //输出第 10 行至文件尾之间的偶数行

4、读取行 与grep对比

sed -n '/^root/p' /etc/passwd //输出以PI 开头的行
和下面的一样
grep '^root' /etc/passwd

5、查找关键字所在行 固定用法//

[root@localhost ~]# sed -n '/the/p' test.txt //输出包含the 的行
[root@localhost ~]# sed -n '4,/the/p' test.txt //输出从第 4 行至第一个包含
 the 的行
[root@localhost ~]# sed -n '/the/=' test.txt
//输出包含the 的行所在的行号,等号(=)用来输出行号
[root@localhost ~]# sed -n '/^PI/p' test.txt //输出以PI 开头的行
[root@localhost ~]# sed -n '/[0-9]$/p' test.txt //输出以数字结尾的行
[root@localhost ~]# sed -n '/<wood>/p' test.txt
//输出包含单词wood 的行,<、>代表单词边界

总结:

<>搜索的比较精确 与grep相比 sed在搜索行功能更加强大的点在于 sed可以从指定行开始过滤然后输出 比方如下这个 [root@localhost ~]# sed -n ‘4,/the/p’ test.txt //输出从第 4 行至第一个包含 the 的行

6、 删除符合条件的文本(d)

[root@localhost ~]# nl test.txt | sed '3d' //删除第 3 行
[root@localhost ~]# nl test.txt | sed '3,5d' //删除第 3~5 行
[root@localhost ~]# nl test.txt |sed '/cross/d'
//删除包含 cross 的行,原本的第 8 行被删除;如果要删除不包含 cross 的行,
用!符号表示取反操作, 如'/cross/!d'
[root@localhost ~]# sed '/^[a-z]/d' test.txt '//删除以小写字母开头的行'
[root@localhost ~]# sed '/.$/d' test.txt '//删除以"."结尾的行'
[root@localhost ~]# sed '/^$/d' test.txt '//删除所有空行'

注 意 : 若 是 删 除 重 复 的 空行 , 即 连 续 的 空 行 只 保 留 一 个 , 执 行“sed-e‘/^KaTeX parse error: Expected group after '^' at position 6: /{n;/^̲/d}’test.txt”命令即可实现。其效果与“cat -s test.txt”相同,n 表示读下一行数据。

总结

nl相当于cat nl配合d删除行 其实加不加nl都一样 // sed 固定用法 不要忘记加/ 删除/cross/ cross是模糊查找 当文本中有cross字段的都删除

7、替换符合条件的文本

在使用 sed 命令进行替换操作时需要用到 s(字符串替换)、c(整行/整块替换)、y(字符转换)命令选项,常见的用法如下所示。

sed 's/the/THE/' test.txt //将每行中的第一个the 替换为 THE
sed 's/l/L/2' test.txt //将每行中的第 2 个 l 替换为 L
sed 's/the/THE/g' test.txt //将文件中的所有the 替换为 THE
sed 's/o//g' test.txt //将文件中的所有o 删除(替换为空串)
sed 's/^/#/' test.txt //在每行行首插入#号
sed '/the/s/^/#/' test.txt //在包含the 的每行行首插入#号
sed 's/$/EOF/' test.txt //在每行行尾插入字符串EOF
sed '3,5s/the/THE/g' test.txt //将第 3~5 行中的所有 the 替换为 THE
sed '/the/s/o/O/g' test.txt //将包含the 的所有行中的 o 都替换为 O

8、 迁移符合条件的文本

在使用 sed 命令迁移符合条件的文本时,常用到以下参数. H:复制到剪贴板; g、G:将剪贴板中的数据覆盖/追加至指定行; w:保存为文件; r:读取指定文件; a:追加指定内容

sed '/the/{H;d};$G' test.txt //将包含the 的行迁移至文件末尾,{;}用于多个操作
sed '1,5{H;d};17G' test.txt //将第 1~5 行内容转移至第 17 行后
sed '/the/w out.file' test.txt //将包含the 的行另存为文件 out.file
sed '/the/r /etc/hostname' test.txt //将文件/etc/hostname 的内容添加到包含 the 的每行以后
sed '3aNew' test.txt //在第 3 行后插入一个新行,内容为New
sed '/the/aNew' test.txt //在包含the 的每行后插入一个新行,内容为 New
sed '3aNew1nNew2' test.txt //在第 3 行后插入多行内容,中间的n 表示换行

9、a模式后插

sed '/hello/aworld' test 表示把word加在含有hello后一行  默认是在后面一行追加
[root@promote opt]# sed '/hello/aworld' test
hello
world

sed '/hello/aworldnshang' test  表示在含有hello 后面一行追加 world westos,但是有n表示换行 
[root@promote opt]# sed '/hello/aworldnshang' test
hello
world
shang
[root@shang opt]# sed '2,4aword' sheng.txt    在2--4行每行插入一个word

10、i模式前插

sed '/hello/iworld' test  表示在含有hello行的前面插入world westos,n表示换行
[root@promote opt]# sed '/hello/iworld' test
world
hello

11、c模式

sed '/hello/chello world' test    表示把含有hello的行替换为hello
[root@promote opt]# sed '/hello/cworld' test
world

sed '/hello/cworldnshang' test   表示把含有hello的行替换为world shang,n表示换行
[root@promote opt]# sed '/hello/cworldnshang' test
world
shang

12、w模式

前面的模式默认是不会改变文件的内容的

sed -n '/bash$/p' /etc/passwd > file     重定向写入文件
[root@promote opt]# sed -n '/bash$/p' /etc/passwd > file
[root@promote opt]# cat file 
root:x:0:0:root:/root:/bin/bash
shang:x:1000:1000:shang:/home/shang:/bin/bash
zhangsan:x:1001:1001::/home/zhangsan:/bin/bash
lisi:x:1002:1002::/home/lisi:/bin/bash
wangwu:x:1003:1003::/home/wangwu:/bin/bash
liuliu:x:1004:1004::/home/liuliu:/bin/bash
stu1:x:1005:1005::/home/stu1:/bin/bash
stu2:x:1006:1006::/home/stu2:/bin/bash
stu3:x:1007:1007::/home/stu3:/bin/bash


sed -n '/bash$/wfile' passwd         在w模式下,可以直接写入文件,这是追加的过程,不会覆盖原文

三、sed中-n和-p以及不加参数

  • 不加参数默认输出全部 包括匹配到的参数
  • -n选项:只显示匹配处理的行(否则会输出所有)(也就是关闭默认的输出)
  • -p选项:打印
[root@centos6 ~]# vim a.txt
[root@centos6 ~]# cat a.txt
asdf;1324;fdsag
1234567890
qwer
asdasdsadasdasdas
[root@centos6 ~]# sed 's/1324/aaaa/' a.txt > b.txt                     首先sed是有一个默认输出的,也就是将所有文件内容都输出,加上命令行中的替换,那么输出结果就是下面这样
[root@centos6 ~]# cat b.txt
asdf;aaaa;fdsag
1234567890
qwer
asdasdsadasdasdas
[root@centos6 ~]# sed 's/1324/aaaa/p' a.txt > b.txt                   这行的意思就是:首先sed默认输出文件全部内容,然后p又将匹配到的内容打印了一遍,也就是会输出两边匹配到的内容
[root@centos6 ~]# cat b.txt
asdf;aaaa;fdsag
asdf;aaaa;fdsag
1234567890
qwer
asdasdsadasdasdas
[root@centos6 ~]# sed -n 's/1324/aaaa/p' a.txt > b.txt                这行就是sed -n屏蔽默认输出然后s替换,p再将匹配到的内容打印出来,所以只显示了一行,也就是匹配到的那一行
[root@centos6 ~]# cat b.txt
asdf;aaaa;fdsag
[root@centos6 ~]# sed -n 's/1324/aaaa/' a.txt > b.txt                   这行就是sed -n选项屏蔽默认输出,s替换,但是没有p就不会将匹配到的内容输出
[root@centos6 ~]# cat b.txt
[root@centos6 ~]# 

sed 不加参数 默认是 输出全部内容 加上 -n 就是指输出匹配到的内容 p是打印 前提是你得匹配到内容 才能p打印出