zoukankan      html  css  js  c++  java
  • 4.6 sed入门

    用法: sed [-i] command 源文件

    其中command由三部分组成为:匹配 命令 操作

    其中命令有s替换,a后面增加行,i前面增加行,d删除行,c替换行,p打印行(与选项-n配合使用)

    选项:-i:结果应用于源文件;-n输出只处理的哪一行

    命令: 1.作用于字符串 

        s/patterm/replace_string/:取代,搭配正则表达式(只取代第一处)

    yang@Ubuntu:~$ echo this thisThisTHIS | sed "s/[iI][sS]/at/"
    that thisThisTHIS

        s/patterm/replace_string/g:取代所有符合条件的

    yang@Ubuntu:~$ echo this thisThisTHIS | sed "s/[iI][sS]/at/g"
    that thatThatTHat

        s/patterm/replace_string/g:取代所有符合条件从第N处开始

    yang@Ubuntu:~$ echo this thisThisTHIS | sed "s/[iI][sS]/at/3g"
    this thisThatTHat

        &:已匹配字符串标记&

    yang@Ubuntu:~$ echo -e "this is a line.\nthis is a Line.\nthis is a LINE." | sed "s/this/[&]/"
    [this] is a line.
    [this] is a Line.
    [this] is a LINE.

        \1:字串匹配标记,\(pattern\)用于匹配子串

    yang@Ubuntu:~$ echo -e "this is a line.\nthis is a Line.\nthis is a LINE." | sed "s/th\(is\)/\1/"
    is is a line.
    is is a Line.
    is is a LINE.

        匹配到的第一个子串\1,第二个子串\2(子串中的+也要使用转义字符,有点小疑问

    yang@Ubuntu:~$ echo newOLD | sed "s/\([a-z]\+\)\([A-Z]\+\)/\2\1/"
    OLDnew

      2.作用于行(扩展其他命令

       删除行:1d,第一行;$d,最后一行;3,5d;删除第3至第5行;/pattern/d,删除匹配的行

    yang@Ubuntu:~$ echo -e "this is a line.\nthis is a Line.\nthis is a LINE." | sed "2d"
    this is a line.
    this is a LINE.
    yang@Ubuntu:~$ echo -e "this is a line.\nthis is a Line.\nthis is a LINE." | sed "/line/d"
    this is a Line.
    this is a LINE.
  • 相关阅读:
    Myeclipse安装svn插件
    Hudson+Maven+Svn搭建持续集成环境
    svn merge和branch分析
    Linux下安装subversion1.6.5和apache2
    C语言中,为什么字符串可以赋值给字符指针变量
    Loadrunner C 编程_1
    oracle解决多表关联分组查询问题
    学习英语
    使用 JMeter 完成常用的压力测试 [转]
    Jmeter零起点学习
  • 原文地址:https://www.cnblogs.com/yangqionggo/p/2868297.html
Copyright © 2011-2022 走看看