Shell 正则表达式基础详解(二)

本文详细介绍了Linux/UNIX系统中的文本处理器sed工具,包括sed的工作流程、常见用法、删除文本、替换文本、迁移文本以及使用脚本编辑文件等。此外,还提到了awk工具的基本用法和示例,以及sort和uniq工具的概述和应用。
摘要由CSDN通过智能技术生成
前言:

上篇博客我们介绍了正则表达式中grep与egrep的元字符功能,此篇博客将介绍文本编辑器

一、文本处理器

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

二、sed工具
2.1 sed 工具介绍

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

2.2 sed工作流程及常见用法
2.2.1 工作流程

读取:sed从输入流(文件、管道、标准输入)中读取以行内容并存储到临时的缓冲区中(又称模式空间)

执行:默认情况下,所有sed命令都在模式空间中顺序地执行,除非指定了行的地址,否则sed命令将会在所有的行依次执行。

显示:发送修改后的内容到输出流。再发送数据后,模式空间将会被请空。在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。

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

2.2.2 sed命令常见用法

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

格式1:sed [选项] ‘操作’ 参数

格式2:sed [选项] -f scriptfile 参数

常见的sed命令选项:

在这里插入图片描述
“操作” 用于指定对文本操作的动作行为,也就是sed的命令。通常情况下是采用的“[n1[n2]]”操作参数的格式。n1、n2是可选的,不一定会存在,代表选择进行操作的行数,如操作需要在5~20行之间进行,则表示为“5,20 动作行为”。

常见的操做:
在这里插入图片描述

2.3 用法示例

本篇博客依然以test.txt 文件进行演示:

[root@localhost opt]# cat test.txt
1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4、The year ahead will test our political establishment to the limit.
5、PI=3.141592653589793238462643383249901429
6、a wood cross!
7、Actions speak louder than words
8、the test is a kidding
9、the man is true
10、#woood # #woooooood # AxyzxyzxyzxyzC
11、I bet this place is really spooky late at night! Misfortunes never come alone/single.
12、wd
13、wod
14、I shouldn't have lett so tast.

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

[root@localhost opt]# sed -n 'p' test.txt 
1、he was short and fat.
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4、The year ahead will test our political establishment to the limit.
5、PI=3.141592653589793238462643383249901429
6、a wood cross!
7、Actions speak louder than words
8、the test is a kidding
9、the man is true
10、#woood # #woooooood # AxyzxyzxyzxyzC
11、I bet this place is really spooky late at night! Misfortunes never come alone/single.
12、wd
13、wod
14、I shouldn't have lett so tast.

示例2:输出第三行

[root@localhost opt]# sed -n '3p' test.txt 
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.

示例3:输出3~5行

[root@localhost opt]# sed -n '3,5p' test.txt 
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
4、The year ahead will test our political establishment to the limit.
5、PI=3.141592653589793238462643383249901429

示例4:输出所有奇数行,n表示读入下一行资料

[root@localhost opt]# sed -n 'p;n' test.txt 
1、he was short and fat.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
5、PI=3.141592653589793238462643383249901429
7、Actions speak louder than words
9、the man is true
11、I bet this place is really spooky late at night! Misfortunes never come alone/single.
13、wod

示例5:输出所有偶数行,n表示读入下一行资料

[root@localhost opt]# sed -n 'n;p' test.txt 
2、He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
4、The year ahead will test our political establishment to the limit.
6、a wood cross!
8、the test is a kidding
10、#woood # #woooooood # AxyzxyzxyzxyzC
12、wd
14、I shouldn't have lett so tast.

示例6:输出第1~5行之间的奇数行(第1、3、5行)

[root@localhost opt]# sed -n '1,5{p;n}' test.txt 
1、he was short and fat.
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
5、PI=3.141592653589793238462643383249901429

需注意,如果输出第2~6行之间的奇数行,如下:

[root@localhost opt]# sed -n '2,6{n;p}' test.txt 
3、the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
5、PI=3.141592653589793238462643383249901429
7、Actions speak louder than words

首先在输入时 n需要与p位置互换
在输出结果后,我们会发现输出了第7行,这是因为”{n;p}“在执行到第6行时,会执行”n“读入下一行资料,然后输出”p“,即第7行的内容。

示例7:输出第10行至文件尾之间的偶数行

[root@localhost opt]# sed -n '10,${p;n}' test.txt 
10、#woood # #woooooood # AxyzxyzxyzxyzC
12、wd
14、I shouldn't have lett so tast.

在执行”sed -n ‘10,${p:n}’ test.txt “命令时,读取的第一行时文件的第10行,直接打印,下一行行是第11行,读取下一行后输出第12行,以此类推,所以输出的偶数行是文件的第10行、12行直至结尾,如有空行在偶数行也会进行输出。

以上是sed命令的基本用法。

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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值