• 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.
  • 相关阅读:
    🔺 Garbage Remembering Exam UVA
    Cows and Cars UVA
    Probability|Given UVA
    Crossing Rivers HDU
    均匀分布和高斯分布
    Race to 1 UVA
    XMPPElementReceipt wait return,
    someone like you,
    第三方统计,
    截获的感觉,
  • 原文地址:https://www.cnblogs.com/yangqionggo/p/2868297.html
Copyright © 2020-2023  润新知