美文网首页
Sed学习笔记1

Sed学习笔记1

作者: dming1024 | 来源:发表于2019-04-24 09:27 被阅读0次

    sed有以下几种常用的工作模式:
    -n,静态处理命令,不涉及的行不打印;
    -e,默认选项,把sed后的输入当做命令来执行;
    -i,会直接修改文件;
    -r,扩展正则表达式。
    直接讲述实例:

    cat sedExample.txt#输出文件查看一下内容,第一行是空行
    
    today is May
    today date is 24th May
    today is Wednesday
    what day is it today
    what a beautiful
    it is the last day of the word
    

    打印第2行

     cat sedExample.txt |sed -n '2p'
     today is May
    

    打印指定的行(含有May字符的行)

    cat sedExample.txt |sed -n '/May/p'
    today is May
    today date is 24th May
    

    打印有day单词的行

     cat sedExample.txt |sed -n '/\<day\>/p'
    what day is it today
    it is the last day of the word
    

    打印有day字符的行

    cat sedExample.txt |sed -n '/day/p'
    today is May
    today date is 24th May
    today is Wednesday
    what day is it today
    it is the last day of the word
    

    打印空行(^$代表空行)

    cat sedExample.txt |sed -n '/^$/p'
    
    

    打印非空行(!代表反选)

    cat sedExample.txt |sed -n '/^$/!p'
    today is May
    today date is 24th May
    today is Wednesday
    what day is it today
    what a beautiful
    it is the last day of the word
    

    打印有day单词的行,以及行号(=显示行号)

    cat sedExample.txt |sed -n '/\<day\>/{=;p}'
    5
    what day is it today
    7
    it is the last day of the word
    

    打印以today开头的行

    cat sedExample.txt |sed -n '/^today/p'
    today is May
    today date is 24th May
    today is Wednesday
    

    打印以May结尾的行

    cat sedExample.txt |sed -n '/May$/p'
    today is May
    today date is 24th May
    

    sed结合正则表达式使用,会有非常强大的文本处理功能

    相关文章

      网友评论

          本文标题:Sed学习笔记1

          本文链接:https://www.haomeiwen.com/subject/imqwgqtx.html