Linux 文本处理工具sed的用法
诺言 2018-05-15 来源 : 阅读 1027 评论 0

摘要:本文主要介绍了Linux 文本处理工具sed是一个强大的工具,通过实际的案例,让我们从中学处理文本的精髓所在,让我们在掌握这个工具对于我们处理文本,提高效率这条路上走得更远,更加熟练的完成这些代码。

本文主要介绍了Linux 文本处理工具sed是一个强大的工具,通过实际的案例,让我们从中学处理文本的精髓所在,让我们在掌握这个工具对于我们处理文本,提高效率这条路上走得更远,更加熟练的完成这些代码。


sed强大的文本处理工具,其工作流程大概如下

常用选项说明

-e<script>或--expression=<script> 以选项中指定的script来处理输入的文本文件。
-f<script文件>或--file=<script文件> 以选项中指定的script文件来处理输入的文本文件。
-h或--help 显示帮助。
-n或--quiet或--silent 仅显示script处理后的结果。
-V或--version 显示版本信息。

操作命令:

a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法。

1、实验环境

[root@localhost test]# pwd

/root/test

[root@localhost test]# ls

fstab  yum.conf

 

[root@localhost test]# cat -n fstab

     1

     2  #

     3  # /etc/fstab

     4  # Created by anaconda on Sun Apr 22 06:26:44 2018

     5  #

     6  # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

     7  # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

     8  #

     9  UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac /                       xfs     defaults        0 0

    10  UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

    11  UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

花样删除:

 #删除1到9行

[root@localhost test]# sed ‘1,9d‘ fstab

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

 

#--------------------------------------------------------------------------------------------------

#删除第3行

[root@localhost test]# sed ‘3d‘ fstab

 

#

# Created by anaconda on Sun Apr 22 06:26:44 2018

#

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac /                       xfs     defaults        0 0

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

 

#--------------------------------------------------------------------------------------------------

#删除从第3行开始 连续5行的数据

[root@localhost test]# sed ‘3,+5d‘ fstab

 

#

UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac /                       xfs     defaults        0 0

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

 

#--------------------------------------------------------------------------------------------------

#删除奇数行

[root@localhost test]# sed ‘1~2d‘ fstab

#

# Created by anaconda on Sun Apr 22 06:26:44 2018

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

#

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

 

#--------------------------------------------------------------------------------------------------

#删除^UUID=7开始的行 到UUID=9第一次出现的行   如果UUID=9没有出现,则一直往下删

[root@localhost test]# sed ‘/^UUID=7/,/^UUID=9/d‘ fstab

 

#

# /etc/fstab

# Created by anaconda on Sun Apr 22 06:26:44 2018

#

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

 

#--------------------------------------------------------------------------------------------------

#删除第5行开始的行 到UUID=9第一次出现的行   如果UUID=9没有出现,则一直往下删

[root@localhost test]# sed ‘5,/^UUID=999/d‘ fstab

 

#

# /etc/fstab

# Created by anaconda on Sun Apr 22 06:26:44 2018

 

#--------------------------------------------------------------------------------------------------

#执行两次操作

[root@localhost test]# sed -e ‘1,3d‘ -e ‘4,6d‘ fstab

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac /                       xfs     defaults        0 0

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

 

#--------------------------------------------------------------------------------------------------

#执行多次操作

[root@localhost test]# sed    ‘1d;2d;3d‘  fstab

# Created by anaconda on Sun Apr 22 06:26:44 2018

#

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac /                       xfs     defaults        0 0

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

print

 #删除第5行开始的行 到UUID=9第一次出现的行  打印一次,其余正常输出

[root@localhost test]# sed ‘5,/^UUID=999/p‘ fstab

 

#

# /etc/fstab

# Created by anaconda on Sun Apr 22 06:26:44 2018

#

#

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

#

UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac /                       xfs     defaults        0 0

UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac /                       xfs     defaults        0 0

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

 

#--------------------------------------------------------------------------------------------------

#删除第5行开始的行 到UUID=9第一次出现的行  看图理解 打印的行先执行,然后所有的数据用静默模式处理  

[root@localhost test]# sed -n  ‘5,/^UUID=999/p‘ fstab

#

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac /                       xfs     defaults        0 0

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

第一行后面插入

 [root@localhost test]# sed   ‘1a \newline‘ fstab

 

newline

#

# /etc/fstab

# Created by anaconda on Sun Apr 22 06:26:44 2018

#

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac /                       xfs     defaults        0 0

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

第三行插入

 [root@localhost test]# sed   ‘3i \newline‘ fstab

newline

 

#

# /etc/fstab

# Created by anaconda on Sun Apr 22 06:26:44 2018

#

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac /                       xfs     defaults        0 0

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

第三行替换

 [root@localhost test]# sed   ‘3c \newline‘ fstab

 

#

newline

# Created by anaconda on Sun Apr 22 06:26:44 2018

#

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac /                       xfs     defaults        0 0

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

把第四行写入到w.txt文件中

 [root@localhost test]# sed   ‘4w /root/test/w.txt‘ fstab

 

#

# /etc/fstab

# Created by anaconda on Sun Apr 22 06:26:44 2018

#

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac /                       xfs     defaults        0 0

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

 

[root@localhost test]# cat w.txt

# Created by anaconda on Sun Apr 22 06:26:44 2018

读取文件内容到第六行下面输出

 [root@localhost test]# sed   ‘6r /root/test/w.txt‘ fstab

 

#

# /etc/fstab

# Created by anaconda on Sun Apr 22 06:26:44 2018

#

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

# Created by anaconda on Sun Apr 22 06:26:44 2018

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac /                       xfs     defaults        0 0

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

为模式匹配到的行打印行号

 [root@localhost test]# sed   ‘6=‘ fstab

 

#

# /etc/fstab

# Created by anaconda on Sun Apr 22 06:26:44 2018

#

6

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac /                       xfs     defaults        0 0

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

正则使用

 [root@localhost test]# sed ‘1,10s/UUID/uuid/‘ fstab

 

#

# /etc/fstab

# Created by anaconda on Sun Apr 22 06:26:44 2018

#

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

uuid=7ceb028a-a8b8-467c-b6d4-36910c06c5ac /                       xfs     defaults        0 0

uuid=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

 

高级用法:

逆序排序

[root@localhost test]# sed  ‘1!G;h;$!d‘  fstab

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac /                       xfs     defaults        0 0

#

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

#

# Created by anaconda on Sun Apr 22 06:26:44 2018

# /etc/fstab

#

实现过程py分析

 mode_space = ‘‘

keep_space = ‘‘

 

with open(‘test2.txt‘,encoding=‘utf-8‘)  as f:

 

    i=0

    for line in f:

        if i == 0:

            mode_space = line

            keep_space = mode_space

        else:

            mode_space = line+keep_space

            keep_space = mode_space

        i += 1

    else:

        #最后一行读完删除

        print(keep_space)

        del keep_space

取出最后一行

[root@localhost test]# sed ‘$!d‘ fstab

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

取出文件后两行

[root@localhost test]# sed  ‘$!N;$!D‘ fstab

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

显示偶数行

[root@localhost test]# sed  -n  ‘n;p‘ fstab

#

# Created by anaconda on Sun Apr 22 06:26:44 2018

# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

#

UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot                   xfs     defaults        0 0

显示奇数行

[root@localhost test]# sed  ‘n;d‘ fstab

 

# /etc/fstab

#

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac /                       xfs     defaults        0 0

UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap                    swap    defaults        0 0

 


本文由 @诺言 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程