zoukankan      html  css  js  c++  java
  • sed

    sed  [option]  '[lines]  [!]command[/RE1/RE2/][flag]  [newfile]'  file

        -e            s           g

        -n            d            "num"

        -f             w             w

                     p            p

                     q

                     a

                     i

                       c

    实例:

    1.   -e:  sed -e 's/RE1/RE2/' -e 's/RE3/RE4/' file1 file2;  等价于: sed 's/RE1/RE2/; s/RE3/RE4/' file1 file2
    2. num:  sed 's/RE1/RE2/2' file1; 只将file1每行中第2次出现的RE1替换为RE2
    3.   -f:   sed -f sedcommandscript file; 从sedcommandscript的每行中读入sed命令作用于file
    4. lines:  sed '3 s/a/A/' file; 只对file的第3行操作
    5. lines:  sed '/^g/ s/a/A/' file; 只对以g开头的行操作
    6. lines:  sed '5,10 s/a/A/' file; 只对5至10行操作
    7. lines:  sed '5,$ s/a/A/' file; 操作从第5行开始到结束,$表示结束
    8. lines:  sed '5,/end/ s/a/A/' file; 从第5行开始至以后第一个含有end的行结束
    9. lines:  sed '/start/,/stop/ s/a/A/' file; 对file中的每一行检测,字符start和stop就像操作的开关,一个文件中可以有多个开关,实验:                                                         echo 1a,1a,2a,2a,1a,2a,1a,3a|tr , '\n'|sed '/1/,/2/ s/a/z/'
    10.    d:  sed '/start,/stop/ d'; d: 删除行,所有有关lines的表达式都适用
    11.   w:  sed 's/RE1/RE2/w newfile' file; 将经过改动的行写入文件newfile
    12.   w:  sed 's/RE1/RE2; w newfile' file; 将经过改动的和没有经过改动的行都写入文件newfile, 且该命令的结果与命令11的结果不同
    13.   w:  sed '/start/,/stop/ w newfile' file; 所有有关lines的表达式对w都适用
    14.   p:  p的用法与w类似,但command p经常与option -n组合使用
    15.   q:  /line/q, quit; echo 1a,2a,3a,4a|tr , '\n'|sed '3q; sed/a/x/' 与 sed 'sed/a/x/; 3q'的输出结果最后一行不同;q前面只应匹配一个地址
    16.   a:  sed '[lines] a string'; 在匹配行的后面插入一行;如果省略lines,就在所有行后面插入
    17.    i:  sed '[lines] i string'; 在匹配行的前面插入一行
    18.   c:  sed '[lines] c string'; 改写匹配行





  • 相关阅读:
    2020.10.6 提高组模拟
    GMOJ 6815. 【2020.10.06提高组模拟】树的重心
    Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1) D. Isolation
    Forethought Future Cup
    Codeforces Round #543 (Div. 2, based on Technocup 2019 Final Round) D. Diana and Liana
    2020.10.07提高组模拟
    2020.10.05提高组模拟
    9.29 联赛组作业
    JZOJ 3978. 寝室管理
    Centos7下安装netstat的方法
  • 原文地址:https://www.cnblogs.com/mphyfin/p/2112111.html
Copyright © 2011-2022 走看看