zoukankan      html  css  js  c++  java
  • shell命令之---sed

    1. sed编辑器基础

      1.1 替换标记

        命令格式:s/pattern/replacement/flags

        $ cat data4.txt
        This is a test of the test script.
        This is the second test of the test script.

        有4种可用的替换标记:

          数字,表明新文本将替换第几处模式匹配的地方;

          g,表明新文本将会替换所有匹配的文本;

          p,表明原先行的内容要打印出来;

          w file,将替换的结果写到文件中。

        在第一类替换中,可以指定sed编辑器用新文本替换第几处模式匹配的地方。
            $ sed 's/test/trial/2' data4.txt
            This is a test of the trial script.
              This is the second test of the trial script

        将替换标记指定为2的结果就是: sed编辑器只替换每行中第二次出现的匹配模式。

        g替换标记使你能替换文本中匹配模式所匹配的每处地方。
          $ sed 's/test/trial/g' data4.txt
                            This is a trial of the trial script.
                            This is the second trial of the trial script.
                  p替换标记会打印与替换命令中指定的模式匹配的行。这通常会和sed的-n选项一起使用。

          $ cat data5.txt
          This is a test line.
          This is a different line.
          $
          $ sed -n 's/test/trial/p' data5.txt
          This is a trial line.

                 -n选项将禁止sed编辑器输出。但p替换标记会输出修改过的行。将二者配合使用的效果就是只输出被替换命令修改过的行。

         w替换标记会产生同样的输出,不过会将输出保存到指定文件中。
          $ sed 's/test/trial/w test.txt' data5.txt
          This is a trial line.
          This is a different line.
          $
          $ cat test.txt
          This is a trial line. 

      1.2 使用地址

        在sed编辑器中有两种形式的行寻址:

          以数字形式表示行区间

          以文本模式来过滤出行  

      1.3 删除行

        命令d执行删除操作。

        可以结合指定行号或是使用模式匹配

        通过特殊的文件结尾字符:
        $ sed '3,$d' data6.txt
        This is line number 1.
        This is line number 2.
        $

        sed编辑器的模式匹配特性也适用于删除命令。
        $ sed '/number 1/d' data6.txt
        This is line number 2.
        This is line number 3.
        This is line number 4.
        $

        说明 记住, sed编辑器不会修改原始文件。你删除的行只是从sed编辑器的输出中消失了。原始文件仍然包含那些“删掉的”行

      1.4 插入和附加文本

        sed编辑器允许向数据流插入和附加文本行。

        插入(insert)命令(i)会在指定行前增加一个新行;

        附加(append)命令(a)会在指定行后增加一个新行。

        命令行格式如下:

        sed '[address]command new line'

        例如:$ echo "Test Line 2" | sed 'iTest Line 1'
           Test Line 1
           Test Line 2
           $

      1.5 转换命令

        转换(transform)命令(y)是唯一可以处理单个字符的sed编辑器命令。转换命令格式如下。
        [address]y/inchars/outchars/

        这里有个使用转换命令的简单例子。
        $ sed 'y/123/789/' data8.txt
        This is line number 7.
        This is line number 8.
        This is line number 9.
        This is line number 4.
        This is line number 7 again.
        This is yet another line.
        This is the last line in the file.

     

     

     

        

        

        


       

     

    不积跬步,无以至千里;不积小流,无以成江海。
  • 相关阅读:
    第三次作业
    第二次作业
    第一次作业—编译原理概述
    第六次课堂作业——正规文法与正规式
    词法分析程序的设计与实现
    1702第四次作业(文法和语文总结与梳理)
    1702第三次作业(语法树,短语,直接短语,句柄)
    软工1702第一次作业(简述编译程序)
    random库的使用
    基本的字符串之切片
  • 原文地址:https://www.cnblogs.com/weidongliu/p/11002001.html
Copyright © 2011-2022 走看看