码迷,mamicode.com
首页 > 系统相关 > 详细

Linux 文本操作之sed

时间:2018-04-27 13:27:49      阅读:164      评论:0      收藏:0      [点我收藏+]

标签: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

Linux 文本操作之sed

标签:linux sed

原文地址:http://blog.51cto.com/marvin89/2108403

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!