sed命令

 linux之sed用法
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换、删除、新增、选取等特定工作,下面先了解一下sed的用法
sed命令行格式为:
         sed [-nefri] ‘command’ 输入文本      

   sed 's/原字符串/替换字符串/'   #替换一行

   sed 's/原字符串/替换字符串/g'  #替换每一行

常用选项:
        -n∶使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到屏幕上。但如果加上 -n 参数后,则只有经过 sed 特殊处理的那一行(或者动作)才会被列出来。
        -e∶直接在指令列模式上进行 sed 的动作编辑;
        -f∶直接将 sed 的动作写在一个档案内, -f filename 则可以执行 filename 内的sed 动作;
        -r∶sed 的动作支援的是延伸型正规表示法的语法。(预设是基础正规表示法语法)
        -i∶直接修改读取的档案内容,而不是由屏幕输出。       

常用命令:
        a   ∶新增(add),    a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
        c   ∶取代(change), c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
        d   ∶删除(delete), 因为是删除啊,所以 d 后面通常不接任何咚咚;
         i   ∶插入(insert),  i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
         p  ∶列印(print),    亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起运作~
         s  ∶取代(substitute,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!

举例:(假设我们有一文件名为file)

  a)增加一行或多行字符串
     [root@localhost sunziying]# cat file
     Hello!
     welcome to my blog.
     end
     [root@localhost sunziying] # sed '1a drink tea'  file      #第一行后增加字符串"drink tea"
     Hello!
     drink tea
     ruby is me,welcome to my blog.
     end
     [root@localhost sunziying] # sed '1,3a drink tea'  file    #第一行到第三行后增加字符串"drink tea"
     Hello!
     drink tea
     welcome to my blog.
     drink tea
     end
     drink tea
     [root@localhost sunziying] # sed '1a drink tea\nor coffee' file   #第一行后增加多行,使用换行符\n
     Hello!
     drink tea
     or coffee
     welcome to my blog.
     end

 

  c)代替一行或多行
     [root@localhost sunziying] # sed '1c Hi' file                #第一行代替为Hi
     Hi
     welcome to my blog.
     end
     [root@localhost sunziying] # sed '1,2c Hi' file             #第一行到第二行代替为Hi
     Hi
     end


     d)删除某行
     [root@localhost sunziying] # sed '1d' file              #删除第一行
     [root@localhost sunziying] # sed '$d' file              #删除最后一行
     [root@localhost sunziying] # sed '1,2d' file           #删除第一行到第二行
     [root@localhost sunziying] # sed '2,$d' file           #删除第二行到最后一行

   

  i)插入
     [root@localhost sunziying] # sed -i '$a bye' file         #在文件中最后一行直接输入"bye"
     [root@localhost sunziying]# cat ab
     Hello!
     welcome to my blog.
     end
     bye


  p)显示某行
.    [root@localhost sunziying] # sed -n '1p' file           #显示第一行
     [root@localhost sunziying] # sed -n '$p' file           #显示最后一行
     [root@localhost sunziying] # sed -n '1,2p' file        #显示第一行到第二行
     [root@localhost sunziying] # sed -n '2,$p' file        #显示第二行到最后一行

  使用模式进行查询
     [root@localhost sunziying] # sed -n '/hello/p' file        #查询包括关键字hello所在所有行
     [root@localhost sunziying] # sed -n '/\$/p' file        #查询包括关键字$所在所有行,使用反斜线\屏蔽特殊含义



  s)替换一行中的某部分
  格式:sed 's/要替换的字符串/新的字符串/g'   (要替换的字符串可以用正则表达式)
     [root@localhost sunziying] # sed -n '/hello/p' ab | sed 's/hello/Hi/g'    #替换hello为Hi
   [root@localhost sunziying] # sed -n '/hello/p' ab | sed 's/hello//g'        #删除hello 

 

原文地址:https://www.cnblogs.com/sunziying/p/6382008.html