• linux系统中sed命令输出指定的行


    1、测试数据如下:

    复制代码
    [root@centos79 test]# seq 10 > a.txt
    [root@centos79 test]# ls
    a.txt
    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    复制代码

    2、提取第2行

    复制代码
    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@centos79 test]# sed -n '2p' a.txt
    2
    复制代码

    3、提取第2行到第7行

    复制代码
    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@centos79 test]# sed -n '2,7p' a.txt
    2
    3
    4
    5
    6
    7
    复制代码

    4、只提取第2行和第7行

    复制代码
    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@centos79 test]# sed -n '2p;7p' a.txt
    2
    7
    复制代码

    5、提取奇数行

    复制代码
    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@centos79 test]# sed -n '1~2p' a.txt
    1
    3
    5
    7
    9
    复制代码

    6、提取偶数行

    复制代码
    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@centos79 test]# sed -n '2~2p' a.txt
    2
    4
    6
    8
    10
    复制代码

    7、提取3倍数行

    复制代码
    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@centos79 test]# sed -n '3~3p' a.txt
    3
    6
    9
    复制代码

    利用正则表达式提取特定行。

    8、测试数据如下:

    复制代码
    [root@centos79 test]# seq 15 > a.txt
    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    复制代码

    9、提取匹配2的行

    复制代码
    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@centos79 test]# sed -n '/2/p' a.txt
    2
    12
    复制代码

    10、提取没有匹配2的行

    复制代码
    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@centos79 test]# sed -n '/2/!p' a.txt
    1
    3
    4
    5
    6
    7
    8
    9
    10
    11
    13
    14
    15
    复制代码

    11、提取同时匹配2和5的行

    复制代码
    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@centos79 test]# sed -n '/2\|5/p' a.txt
    2
    5
    12
    15
    复制代码

    12、提取没有匹配2或者5的行

    复制代码
    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@centos79 test]# sed -n '/2\|5/!p' a.txt
    1
    3
    4
    6
    7
    8
    9
    10
    11
    13
    14
    复制代码

    13、提取以2开头的行

    复制代码
    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@centos79 test]# sed -n '/^2/p' a.txt
    2
    复制代码

    13、提取以2结尾的行

    复制代码
    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@centos79 test]# sed -n '/2$/p' a.txt
    2
    12
    复制代码

    14、同时提取以2开头或者以5开头的行

    复制代码
    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@centos79 test]# sed -n '/^2\|^5/p' a.txt
    2
    5
    复制代码

    15、同时提取以2和5结尾的行

    复制代码
    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@centos79 test]# sed -n '/2$\|5$/p' a.txt
    2
    5
    12
    15
    复制代码

    16、提取以1开头以5结尾的行

    复制代码
    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    13340
    213435
    1332425
    13
    1434
    13245
    [root@centos79 test]# sed -n '/^1.*5$/p' a.txt
    1332425
    13245
    复制代码

    17、提取3的倍数行以外的行

    [root@centos79 test]# cat a.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@centos79 test]# sed -n '3~3!p' a.txt
    1
    2
    4
    5
    7
    8
    10

    原文链接:https://www.cnblogs.com/liujiaxin2018/p/14984096.html

  • 相关阅读:
    会议记录补充5月9日
    会议记录补充5月11日
    每日会议记录5月6日
    SQL Server 日期函数
    Jvascript运算符
    For循环
    JS数据类型
    初识Javascript
    检测浏览器版本(综合整理)
    自己实现一个数组的slice方法
  • 原文地址:https://www.cnblogs.com/superbaby11/p/16556602.html
Copyright © 2020-2023  润新知