Shell脚本——三剑客之Sed篇

三剑客之sed篇

引言

在Linux/UNIX 系统中包含很多种类的文本处理器或文本编辑器,其中包括我们之前学习过的VIM 编辑器与grep 等。而 grep,sed,awk 更是Shell 编程中经常用到的文本处理工具, 被称之为Shell 编程三剑客!

一、Sed编辑器

1.1Sed概述

  • sed(Stream EDitor)是一个强大而简单的文本解析转换工具,可以读取文本,并根据指定的条件对文本内容进行编辑删除、替换、添加、移动等),最后输出所有行或者仅输出处理的某些行。
  • sed也可以在无交互的情况下实现相当复杂的文本处理操作,被广泛应用于Shell脚本中,用以完成各种自动化处理任务

1.2 sed工作流程

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

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

ps:默认情况下所有的 sed 命令都是在模式空间内执行的,因此输入的文件并不会发生任何变化,除非是用重定向存储输出。

1.3 sed命令常见用法

通常情况下调用 sed 命令有两种格式,如下所示。
其中,“参数”是指操作的目标文件, 当存在多个操作对象时用,文件之间用逗号“,”分隔;而 scriptfile 表示脚本文件,需要用“-f” 选项指定,当脚本文件出现在目标文件之前时,表示通过指定的脚本文件来处理输入的目标文件。

1.3.1 基本格式

sed [选项] ‘操作’ 参数
sed [选项] -f scriptfile 参数
sed -e ‘操作’ 文件1 文件2
sed -n -e ‘操作’ 文件1 文件2
sed -f 脚本文件 文件1 文件2
sed -i -e ‘操作’ 文件1 文件2

1.3.2 sed命令常用选项

选项 功能
-e 或–expression=: 表示用指定命令或者脚本来处理输入的文本文件
-f 或–file=: 表示用指定的脚本文件来处理输入的文本文件。
-h 或–help 显示帮助。
-n、–quiet 或 silent 表示仅显示处理后的结果
-i.bak 直接编辑文本文件。
-r, -E 使用扩展正则表达式
-s 将多个文件视为独立文件,而不是单个连续的长文件流

1.3.3 sed常用的操作命令

“操作”用于指定对文件操作的动作行为,也就是 sed 的命令。
通常情况下是采用的“[n1[,n2]]”操作参数的格式。
n1、n2 是可选的,代表选择进行操作的行数,如操作需要在 520 行之间进行,则表示为“520 动作行为”。
常见的操作包括以下几种:
s∶替换,替换指定字符。

d∶删除,删除选定的行。

a∶ 增加,在当前行下面增加一行指定内容。

i∶ 插入,在选定行上面插入一行指定内容。

c∶ 替换,将选定行替换为指定内容。

Y∶ 字符转换,转换前后的字符长度必须相同。

p∶ 打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容; 如果有非打印字符,则以 AscII码输出。其通常与_n"选项一起使用。

=∶打印行号。

l(小写L)∶打印数据流中的文本和不可打印的ASCII字符(比如结束符s、制表符\t)

二. sed命令使用

2.1 打印内容

2.1.1 打印全部内容

sed ‘ ’交互模式输入一行,自动打印一行相同的

sed -n ‘ ’ 交互模式输入一行,关闭自动打印

sed -n ‘ p’加上p又恢复自动打印

[root@localhost awk]# sed ' '
11
11
22
22
33
33
^C
[root@localhost awk]# sed -n ' '
11
22
33
^C
[root@localhost awk]# sed -n ' p '
11
11
22
22
33
33
^C

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.1.2 查看文件,打印全部内容

在这里插入图片描述
在这里插入图片描述

2.1.3 打印指定行内容

[root@localhost ~]# sed -n ‘3p’ test.txt //输出第 3 行
在这里插入图片描述

2.1.4 打印指定第几行到第几行内容

[root@localhost ~]# sed -n ‘3,5p’ test.txt //输出 3-5 行
在这里插入图片描述

2.1.5 输出所有奇数行

[root@localhost ~]# sed -n ‘p;n’ test.txt //输出所有奇数行,n 表示读入下一行资料
在这里插入图片描述

2.1.6 输出所有偶数行

[root@localhost ~]# sed -n ‘n;p’ test.txt //输出所有偶数行,n 表示读入下一行资料
在这里插入图片描述

2.1.7 输出第 1-5 行之间的奇数行

[root@localhost ~]# sed -n ‘1,5{p;n}’ test.txt //输出第 1-5 行之间的奇数行(第 1、3、5 行) he was short and fat.
在这里插入图片描述

2.1.8 输出第 10 行至文件尾之间的偶数行

[root@localhost ~]# sed -n ‘10,${n;p}’ test.txt //输出第 10 行至文件尾之间的偶数行
#woood # AxyzxyzxyzxyzC
Misfortunes never come alone/single.
在这里插入图片描述

在执行“sed -n‘10,${n;p}’test.txt”命令时,读取的第 1 行是文件的第 10 行,读取的第 2 行是文件的第11 行,依此类推,所以输出的偶数行是文件的第 11 行、13 行直至文件结尾, 其中包括空行。

2.1.9 直接显示第二行内容

ifconfig ens33|sed -n 2p   #直接显示第二行内容

在这里插入图片描述

以上是 sed 命令的基本用法,sed 命令结合正则表达式时,格式略有不同,正则表达式以“/”包围。
例如,以下操作是 sed 命令与正则表达式结合使用的示例。

2.1.10输出包含the 的行

[root@localhost ~]# sed -n '/the/p' test.txt	//输出包含the 的行
the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
The year ahead will test our political establishment to the limit.

2.1.11 输出从第 4 行至第一个包含 the 的行

[root@localhost ~]# sed -n '4,/the/p' test.txt	//输出从第 4 行至第一个包含 the 的行
the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.

2.1.12 输出包含the 的行所在的行号,等号(=)用来输出行号

 [root@localhost ~]# sed -n '/the/=' test.txt
//输出包含the 的行所在的行号,等号(=)用来输出行号
4
5
6

2.1.13 输出以PI 开头的行

[root@localhost ~]# sed -n '/^PI/p' test.txt	//输出以PI 开头的行
PI=3.141592653589793238462643383249901429
[root@localhost ~]# sed -n '/[0-9]$/p' test.txt	//输出以数字结尾的行PI=3.141592653589793238462643383249901429

2.1.14 输出包含单词wood 的行,<、>代表单词边界

[root@localhost ~]# sed -n '/\<wood\>/p' test.txt    //输出包含单词wood 的行,\<、\>代表单词边界
a wood cross!

三.删除符合条件的文本(d)

3.1 删除第三行

[root@localhost ~]# nl test.txt | sed '3d'	//删除第 3 行
1he was short and fat.
2He was wearing a blue polo shirt with black pants.
4the tongue is boneless but it breaks bones.12!
5google is the best tools for search keyword.
6The year ahead will test our political establishment to the limit.
……	//省略部分内容

在这里插入图片描述

3.2 删除第 3~5 行

[root@localhost ~]# nl test.txt | sed '3,5d'	//删除第 3~5 行
1he was short and fat.
2He was wearing a blue polo shirt with black pants.
6 The year ahead will test our political establishment to the limit. 7 PI=3.141592653589793238462643383249901429
8a wood cross!

在这里插入图片描述

3.3 删除包含 cross 的行

[root@localhost ~]# nl test.txt |sed '/cross/d'
//删除包含 cross 的行,原本的第 8 行被删除;如果要删除不包含 cross 的行,用!符号表示取反操作, 如'/cross/!d'

在这里插入图片描述

3.4 删除以小写字母开头的行

[root@localhost ~]# sed '/^[a-z]/d' test.txt	//删除以小写字母开头的行
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
The year ahead will test our political establishment to the limit. PI=3.141592653589793238462643383249901429
Actions speak louder than words

在这里插入图片描述

3.5 删除以“.”结尾的行

[root@localhost ~]# sed '/\.$/d' test.txt	//删除以"."结尾的行
the tongue is boneless but it breaks bones.12!
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words

在这里插入图片描述

3.6 删除所有空行

[root@localhost ~]# sed '/^$/d' test.txt	//删除所有空行
he was short and fat.
He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
The year ahead will test our political establishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words #woood #

在这里插入图片描述

ps : 若 是 删 除 重 复 的 空行 , 即 连 续 的 空 行 只 保 留 一 个 , “cat -s test.txt”。

四.替换符号条件的文本

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

4.1 将每行中的第一个the 替换为 THE

sed ‘s/the/THE/’ test.txt //将每行中的第一个the 替换为 THE

在这里插入图片描述

4.2 将每行中的第 2 个 l 替换为 L

sed ‘s/l/L/2’ test.txt //将每行中的第 2 个 l 替换为 L

在这里插入图片描述

4.3 将文件中的所有the 替换为 THE

sed ‘s/the/THE/g’ test.txt //将文件中的所有the 替换为 THE

在这里插入图片描述

4.4 将文件中的所有o 删除(替换为空串)

sed ‘s/o//g’ test.txt //将文件中的所有o 删除(替换为空串)

在这里插入图片描述

4.5 在每行行首插入#号

sed ‘s/^/#/’ test.txt //在每行行首插入#号

在这里插入图片描述

4.6 sed ‘/the/s/^/#/’ test.txt //在包含the 的每行行首插入#号

sed ‘/the/s/^/#/’ test.txt //在包含the 的每行行首插入#号

在这里插入图片描述

4.7 在每行行尾插入字符串EOF

sed ‘s/$/EOF/’ test.txt //在每行行尾插入字符串EOF

在这里插入图片描述

4.8 将第 3~5 行中的所有 the 替换为 THE

sed ‘3,5s/the/THE/g’ test.txt //将第 3~5 行中的所有 the 替换为 THE

在这里插入图片描述

4.9将包含the 的所有行中的 o 都替换为 O

sed ‘/the/s/o/O/g’ test.txt //将包含the 的所有行中的 o 都替换为 O

在这里插入图片描述

五.迁移符合条件的文本

在使用 sed 命令迁移符合条件的文本时,常用到以下参数.

选项 功能
H 复制到剪贴板;
g、G 将剪贴板中的数据覆盖/追加至指定行;
w 保存为文件;
r 读取指定文件;
a 追加指定内容。具体操作方法如下所示。
I,i 忽略大小写

5.1 将包含the 的行迁移至文件末尾,{;}用于多个操作

sed ‘/the/{H;d};$G’ test.txt //将包含the 的行迁移至文件末尾,{;}用于多个操作

在这里插入图片描述

5.2 将包含the 的行另存为文件 out.file

sed ‘/the/w out.file’ test.txt //将包含the 的行另存为文件 out.file

在这里插入图片描述

5.3 将文件/etc/hostname 的内容添加到包含 the 的每行以后

sed ‘/the/r /etc/hostname’ test.txt //将文件/etc/hostname 的内容添加到包含 the
的每行以

在这里插入图片描述

5.4 在第 3 行后插入一个新行,内容为New

sed ‘4aKY19-GOOD’ test.txt //在第 3 行后插入一个新行,内容为New

在这里插入图片描述

5.5 在第 3 行后插入多行内容,中间的\n 表示换行

sed ‘3aNew1\nNew2’ test.txt //在第 3 行后插入多行内容,中间的\n 表示换行

在这里插入图片描述

六.使用脚本编辑文件

使用 sed 脚本将多个编辑指令存放到文件中(每行一条编辑指令),通过“-f”选项来调用。

6.1 将第 1~5 行内容转移至第 16 行后

sed ‘1,5{H;d};16G’ test.txt //将第 1~5 行内容转移至第 16 行后

在这里插入图片描述

扩展小福利

sed 直接操作文件示例
编写一个脚本,用来调整 vsftpd 服务配置,要求禁止匿名用户,但允许本地用户(也允许写入)。

[root@lo calhost ~]# vim local_only_ftp.sh
#!/bin/bash
# 指定样本文件路径、配置文件路径
SAMPLE="/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.conf "
CONFIG="/etc/vsftpd/vsftpd.conf"
# 备份原来的配置文件,检测文件名为/etc/vsftpd/vsftpd.conf.bak 备份文件是否存在, 若不存在则使用 cp 命令进行文件备份
[ ! -e "$CONFIG.bak" ] && cp $CONFIG $CONFIG.bak # 基于样本配置进行调整,覆盖现有文件
sed -e '/^anonymous_enable/s/YES/NO/g' $SAMPLE > $CONFIG

sed -i -e '/^local_enable/s/NO/YES/g' -e '/^write_enable/s/NO/YES/g' $CONFIG 
grep "listen" $CONFIG || sed -i '$alisten=YES' $CONFIG
# 启动vsftpd 服务,并设为开机后自动运行
systemctl restart vsftpd systemctl enable vsftpd
[root@localhost ~]# chmod +x local_only_ftp.sh

sed分组操作

当我们需要对一行数据进行多次操作的时候我们可以使用{
    
    }进行分组
1) sed '/root/{s/root/ROOT/;s/x/X/g}' /tmp/passwd

2) sed -ne '/root/{s/root/ROOT/;s/x/X/g}' -ne '1,10p' /tmp/passwd

3)echo 123abcxyz |sed -r 's/(123)(abc)(xyz)/\1/'  ###分组 s//代表查找替换  ()代表分组    \1 代表留下的组

4)ifconfig ens33|sed -rn '2s/.*inet ([0-9.]+) .*/\1/p'

5)sed -r s/^[\t]*/#/ /etc/hosts.bak

经典案例

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
我的妈呀!累死我了!

猜你喜欢

转载自blog.csdn.net/weixin_59663288/article/details/124657396