shell编程之正则表达式(sed)

一:sed概述

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

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

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

二:sed命令常见用法

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

  • sed [选项] ‘操作’ 参数
  • sed [选项] -f scriptfile 参数

常见的 sed 命令选项主要包含以下几种:

  • -e 或–expression=:允许多个脚本指令被执行
  • -f 或–file=:表示用指定的脚本文件来处理输入的文本文件
  • -n、–quiet 或 silent:表示仅显示处理后的结果
  • -i:直接编辑文本文件
  • -r 在脚本中使用扩展正则表达式

sed命令由定位文本行和sed编辑命令俩部分组成
sed编辑命令对定位文本进行操作,sed定位方式主要有俩种

  • 1.行号,指定一行或者多行范围
  • 2.使用正则表达式

ed命令定位文本方法:

  • x x指定行号
  • x,y 指定x-y行范围
  • /pattern/ 查看包含的行
  • /pattern/pattern/ 查看包含俩个匹配的行
  • /pattern/,x 从paeertn包含行到x行之间的行
  • x,y! 不包含x,y行

sed编辑方法主要为增删改查打印

  • p 打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以 ASCII 码输出。其通常与“-n”选项一起使用
  • = 打印文件行号
  • a\ 增加,在当前行下面增加一行指定内容
  • i\ 插入,在选定行上面插入一行指定内容
  • d 删除,删除选定的行
  • c\ 替换,将选定行替换为指定内容
  • s 替换,替换指定字符
  • r 从另一个文本中读入文本
  • w 保存为文件
  • y 字符转换
  • H 复制到剪贴板
  • g 将剪贴板中的数据覆盖至指定行
  • G 将剪贴板中的数据追加至指定行

三:用法案例

以 test.txt 文件为例进行演示

[root@server ~]# cat 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 # 
#woooooood # 
AxyzxyzxyzxyzC
I bet this place is really spooky late at night! 
Misfortunes never come alone/single.
I shouldn't have lett so tast.

3.1:输出符合条件的文本

#输出第三行
[root@server ~]# sed -n '3p' test.txt 
The home of Football on BBC Sport online.
#输出3-5行
[root@server ~]# sed -n '3,5p' test.txt 
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.
#输出所有奇数行,n表示读入下一行
[root@server ~]# sed -n 'p;n' test.txt 
he was short and fat.
The home of Football on BBC Sport online.
google is the best tools for search keyword.
PI=3.141592653589793238462643383249901429
Actions speak louder than words
#woood # 
AxyzxyzxyzxyzC
Misfortunes never come alone/single.
#输出所有偶数行,n表示读入下一行
[root@server ~]# sed -n 'n;p' test.txt 
He was wearing a blue polo shirt with black pants. 
the tongue is boneless but it breaks bones.12! 
The year ahead will test our political establishment to the limit.
a wood cross!

#woooooood # 
I bet this place is really spooky late at night! 
I shouldn't have lett so tast.
#输出第 1~5 行之间的奇数行(第 1、3、5 行) 
[root@server ~]# sed -n '1,5{p;n}' test.txt 
he was short and fat.
The home of Football on BBC Sport online.
google is the best tools for search keyword.
#输出第 10 行至文件尾之间的偶数行
[root@server ~]# sed -n '10,${n;p}' test.txt 
#woood # 
AxyzxyzxyzxyzC
Misfortunes never come alone/single.

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

sed 命令结合正则表达式时,格式略有不同,正则表达式以“/”包围。

#输出包含the 的行
[root@server ~]# sed -n '/the/p' test.txt 
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.
#输出从第 4 行至第一个包含 the 的行
[root@server ~]# sed -n '4,/the/p' test.txt 
the tongue is boneless but it breaks bones.12! 
google is the best tools for search keyword.
#输出包含the 的行所在的行号,等号(=)用来输出行号
[root@server ~]# sed -n '/the/=' test.txt 
4
5
6
#输出以PI 开头的行
[root@server ~]# sed -n '/^PI/P' test.txt 
PI=3.141592653589793238462643383249901429
#输出以数字结尾的行
[root@server ~]# sed -n '/[0-9]$/P' test.txt 
PI=3.141592653589793238462643383249901429
#输出包含单词wood 的行,\<、\>代表单词边界
[root@server ~]# sed -n '/\<wood\>/P' test.txt 
a wood cross!

3.2:删除符合条件的文本(d)

#nl命令用于计算文件的行数
[root@server ~]# nl test.txt | sed '3d'   #删除第三行
[root@server ~]# nl test.txt | sed '3,5d'	#删除第 3~5 行
[root@server ~]# nl test.txt |sed '/cross/d'  #删除包含cross的行
[root@server ~]# nl test.txt |sed '/cross/!d'  #删除不包含cross的行
[root@server ~]# sed '/^[a-z]/d' test.txt	#删除以小写字母开头的行
[root@server ~]# sed '/\.$/d' test.txt	  #删除以"."结尾的行
[root@server ~]# sed '/^$/d' test.txt	  #删除所有空行
[root@server ~]# sed  -e '/^$/{n;/^$/d}' test.txt	  #删 除 重 复 的 空行 , 即 连 续 的 空 行 只 保 留 一 个

3.3:替换符合条件的文本

在使用 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

3.4:迁移符合条件的文本

  • 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 ‘3aNew1\nNew2’ test.txt //在第 3 行后插入多行内容,中间的\n 表示换行

3.5:使用脚本编辑文件

使用 sed 脚本将多个编辑指令存放到文件中(每行一条编辑指令),通过“-f”选项来调用。例如执行以下命令即可将第 1~5 行内容转移至第 17 行后。
sed ‘1,5{H;d};17G’ test.txt #将第 1~5 行内容转移至第 17 行后
以上操作可以改用脚本文件方式:

[root@localhost ~]# vi opt.list
1,5H
1,5d
17G
[root@localhost ~]# sed -f opt.list test.txt
The year ahead will test our political establishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words


#woood # #woooooood # AxyzxyzxyzxyzC
I bet this place is really spooky late at night! Misfortunes never come alone/single.
I shouldn't have lett so tast.


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.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shell编程中的正则表达式是一种用于匹配和操作字符串的强大工具。它使用特定的语法规则来描述和匹配符合某个模式的字符串。在Shell编程中,常用的工具如grep、sed和awk都支持使用正则表达式进行模式匹配和文本处理。正则表达式由普通字符和特殊字符(元字符)组成。普通字符指的是任意字母、数字或其他字符,而元字符具有特殊的意义和功能,用于表示模式中的特定字符或符号。通过使用正则表达式,可以实现字符串的匹配、替换、提取等操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Shell编程正则表达式](https://blog.csdn.net/weixin_51099370/article/details/124568808)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Linux操作系统——Shell编程 正则表达式](https://blog.csdn.net/weixin_46411355/article/details/125628828)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值