• Linux sed 命令详解


    Linux sed 命令是利用脚本处理文本文件。
    sed 可按照脚本的指令来处理、编辑文本文件。
    sed 主要用于自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等。

    语法:

    sed [-hnV][-e<script>][-f<script文件>][文本文件]

    参数说明:

    -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,20s/old/new/g 就是啦!

    元字符集:

    ^ 指定行的开始
    $ 指定行的结尾
    . 匹配一个非换行符的字符
    * 匹配零个或多个字符
    [] 匹配指定字符内的任一字符
    [^] 匹配不在指定字符内的任一字符
    .. 保存已匹配的字符
    &  s/super/YY&yy/   super变成YYsuperyy   & 保存搜索字符用来替换其他字符
    < 词首定位符 /<my/  匹配包含以my开头的单词的行
    > 词尾定位符 /my>/  匹配包含以my结尾的单词的行
    x{m} 连续m个x /9{5}/ 匹配包含连续5个9的行
    x{m,} 至少m个x /9{5,}/  匹配包含至少连续5个9的行
    x{m,n} 至少m个,但不超过n个x /9{5,7}/  匹配包含连续5到7个9的行

    实例:
    在testfile文件第四行后添加一行,并将结果输出到标准输出,在命令行提示符输入如下命令:

    sed -e 4ahellowolrd testfile

    查看testfile 中的内容:

    [root@127-0-0-1 yoon]# cat testfile.txt
    HELLO LINUX!
    Linux is a free unix-type opterating system.
    This is a linux testfile!
    good good study!
    Linux test

    使用sed命令后,输出结果如下:

    [root@127-0-0-1 yoon]# sed -e 4ahelloworld testfile.txt
    HELLO LINUX!
    Linux is a free unix-type opterating system.
    This is a linux testfile!
    good good study!
    helloworld
    Linux test
    [root@127-0-0-1 yoon]# sed -e '4ahello world' testfile.txt  (有空格需要单引号)
    HELLO LINUX!
    Linux is a free unix-type opterating system.
    This is a linux testfile!
    good good study!
    hello world
    Linux test

    以行为单位的新增和删除

    删除
    将testfile的内容列出并且打印行号,同时删除第4-5行删除

    [root@127-0-0-1 yoon]# nl testfile.txt
         1    HELLO LINUX!
         2    Linux is a free unix-type opterating system.
         3    This is a linux testfile!
         4    good good study!
         5    hello world
         6    Linux test
    
    [root@127-0-0-1 yoon]# nl testfile.txt | sed '4,5d'
         1    HELLO LINUX!
         2    Linux is a free unix-type opterating system.
         3    This is a linux testfile!
         6    Linux test

    sed的动作为'4,5d',d是删除,因为4-5行删除了,所以显示的数据没有4-5行,另外,原本应该要下达 sed -e 才对,没有 -e 也可以。同事注意,sed 后面接的动作,务必以 '' 两个单引号扩住。

    只删除第二行:

    [root@127-0-0-1 yoon]# nl testfile.txt | sed '2d'
         1    HELLO LINUX!
         3    This is a linux testfile!
         4    good good study!
         5    hello world
         6    Linux test

    删除第三行到最后一行:

    [root@127-0-0-1 yoon]# nl testfile.txt | sed '3,$d'    ($代表最后一行)
         1    HELLO LINUX!
         2    Linux is a free unix-type opterating system.

    新增

    在第二行后面加上drink milk 字样(亦即是加在第三行):

    [root@127-0-0-1 yoon]# nl testfile.txt | sed '2a drink milk'
         1    HELLO LINUX!
         2    Linux is a free unix-type opterating system.
    drink milk
         3    This is a linux testfile!
         4    good good study!
         5    hello world
         6    Linux test

    如果加入到第二行前:

    [root@127-0-0-1 yoon]# nl testfile.txt | sed  '2i drink milk'
         1    HELLO LINUX!
    drink milk
         2    Linux is a free unix-type opterating system.
         3    This is a linux testfile!
         4    good good study!
         5    hello world
         6    Linux test

    如果是增加两行以上,在第二行后面加入两行字,例如,drink milk,eat pig :

    [root@127-0-0-1 yoon]# nl testfile.txt | sed '2a drink milk  (每一行之间都必须要以反斜杠『  』来进行新行的添加)
    > eat pig'
         1    HELLO LINUX!
         2    Linux is a free unix-type opterating system.
    drink milk
    eat pig
         3    This is a linux testfile!
         4    good good study!
         5    hello world
         6    Linux test
    
    [root@127-0-0-1 yoon]# nl testfile.txt | sed '2a drink milk
    eat pig'  (
    换行符)
         1    HELLO LINUX.
         2    This is a linux testfile.
    drink milk
    eat pig
         3    good good study.
         4    hello world.
         5    Linux test.
         6    #This is good !

    追加一行的话,不需要换行符 ,只有追加多行的情况下才需要换行符,最后一行也无需添加换行符,添加的话会多出一个空格

    在第四行添加一行:

    [root@127-0-0-1 yoon]# sed -e '4a new world' testfile.txt
    HELLO LINUX.
    This is a linux testfile.
    good good study.
    hello world.
    new world
    Linux test.
    #This is good !

    在第四行追加两行:

    [root@127-0-0-1 yoon]# sed -e '4a new world
    old world' testfile.txt
    HELLO LINUX.
    This is a linux testfile.
    good good study.
    hello world.
    new world
    old world
    Linux test.
    #This is good !

    在第四行追加三行(两行文字和一行空格):

    [root@127-0-0-1 yoon]# sed -e '4a new world
    old world
    ' testfile.txt
    HELLO LINUX.
    This is a linux testfile.
    good good study.
    hello world.
    new world
    old world
    
    Linux test.
    #This is good !

    添加一个空行:

    [root@127-0-0-1 yoon]# sed -e '4a \' testfile.txt
    HELLO LINUX.
    This is a linux testfile.
    good good study.
    hello world.
    
    Linux test.
    #This is good !

    [root@127-0-0-1 yoon]# sed -e '4a \n' testfile.txt
    HELLO LINUX.
    This is a linux testfile.
    good good study.
    hello world.


    Linux test.
    #This is good !

    以行为单位的替换和显示

    将第3-4行替换为 No 2-5 number

    [root@127-0-0-1 yoon]# nl testfile.txt | sed '2,5c No 2-5 number' (通过这个方法可以取代整行)
         1    HELLO LINUX!
    No 2-5 number
         6    Linux test

    仅列出第2-3行:

    [root@127-0-0-1 yoon]# nl testfile.txt | sed -n '2,3p'
         2    Linux is a free unix-type opterating system.
         3    This is a linux testfile!

    数据的搜寻并显示

    搜索 linux 关键字的行:

    [root@127-0-0-1 yoon]# nl testfile.txt | sed '/linux/p'    (如果linux找到,除了输出所有行,还会匹配输出行)
         1    HELLO LINUX!
         2    Linux is a free unix-type opterating system.
         3    This is a linux testfile!
         3    This is a linux testfile!
         4    good good study!
         5    hello world
         6    Linux test

    使用 -n 的时候只输出包含模板的行

    [root@127-0-0-1 yoon]# nl testfile.txt | sed -n '/linux/p'
         3    This is a linux testfile!

    数据的搜寻并删除

    删除testfile中包含linux的行,其他行输出:

    [root@127-0-0-1 yoon]# nl testfile.txt | sed '/linux/d'
         1    HELLO LINUX!
         2    Linux is a free unix-type opterating system.
         4    good good study!
         5    hello world
         6    Linux test

    数据的搜寻并执行命令

    搜索 linux 对应的行,执行花括号中的命令,每个命令用分号分隔,并把 testfile 替换成 newtestfile,再输出这行:

    [root@127-0-0-1 yoon]# nl testfile.txt | sed -n '/linux/{s/testfile/newtestfile/;p;q}'   (最后的 q 是退出)
         3    This is a linux newtestfile!

    数据的搜寻并替换

    除了整行的处理模式之外,sed 还可以用行为单位进行部分数据的搜寻并替换,基本上 sed 的搜寻与替代的 vi 相当的类似,有点像这样:
    sed 's/要被取代的字符串/新的字符串/g'

    查看 newtestfile 文件内容:

    [root@127-0-0-1 yoon]# cat newtestfile.txt
    eth0 Link encap:Ethernet HWaddr 00:90:CC:A6:34:84
    inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
    inet6 addr: fe80::290:ccff:fea6:3484/64 Scope:Link
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

    本机的ip是192.168.1.100,将 IP 前面的部分予以删除:

    [root@127-0-0-1 yoon]# cat newtestfile.txt | grep 'inet addr:' | sed 's/^.*addr://g'
    192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

    接下来则是删除后续的部分,亦即: 192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0,将 IP 后面的部分予以删除:

    [root@127-0-0-1 yoon]# cat newtestfile.txt | grep 'inet addr:' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'
    192.168.1.100

    多点编辑命令

    sed -e 'cmd' -e 'cmd' filename
    一条 sed 命令,将第三行到末尾的数据删除,并将opterating替换成 newopterating:

    [root@127-0-0-1 yoon]# nl testfile.txt
         1    HELLO LINUX!
         2    Linux is a free unix-type opterating system.
         3    This is a linux testfile!
         4    good good study!
         5    hello world
         6    Linux test
    多条sed:
    [root@127-0-0-1 yoon]# nl testfile.txt | sed '3,$d' | sed 's/opterating/newopterating/'
         1    HELLO LINUX!
         2    Linux is a free unix-type newopterating system.
    一条sed:
     [root@127-0-0-1 yoon]# nl testfile.txt | sed -e '3,$d' -e  's/opterating/newopterating/'
         1    HELLO LINUX!
         2    Linux is a free unix-type newopterating system.
    ( -e 表示多点编辑,第一个 -e 编辑第三行到最后一行删除,第二个 -e 搜索 opterating替换成 newopterating)

    直接修改文件内容

    将 testfile 文件中的每一行结尾为 . 修改为 !:

    [root@127-0-0-1 yoon]# cat testfile.txt
    HELLO LINUX.
    This is a linux testfile.
    good good study.
    hello world.
    Linux test.
    
    [root@127-0-0-1 yoon]# sed 's/.$/!/g' testfile.txt
    HELLO LINUX!
    This is a linux testfile!
    good good study!
    hello world!
    Linux test!

    利用 sed 直接在最后一行输入 #This is good !

    [root@127-0-0-1 yoon]# sed -i '$a #This is good !' testfile.txt
    
    [root@127-0-0-1 yoon]# cat testfile.txt
    HELLO LINUX.
    This is a linux testfile.
    good good study.
    hello world.
    Linux test.
    #This is good !
  • 相关阅读:
    Generate SQL from Excel
    ASP.NET Web API系列教程目录
    进阶篇:以IL为剑,直指async/await
    30分钟?不需要,轻松读懂IL
    进程简介
    二维码详解
    通过IL分析C#中的委托、事件、Func、Action、Predicate之间的区别与联系
    我是一个线程
    ServiceLocator 简单示例(转)
    特性(C#)
  • 原文地址:https://www.cnblogs.com/hankyoon/p/12599698.html
Copyright © 2020-2023  润新知