学习使用sed

来源:互联网 发布:nodejs 数组去重 编辑:程序博客网 时间:2024/05/21 15:04

学习sed的使用
 说明:sed是一种非交互式的流编辑器,通过转换修改流经它的文本,但是sed是不会修改文件本身的,sed的处理单位是以行为单位
 使用方法: sed [OPTIONS] 'COMMAND' FILE
 
 使用-e 或者使用分号可以多次执行sed命令选项
 
 练习脚本是:/etc/testdir/sed.txt
 
 1、找到文件中的this,并且用that替换;找到line用Line替换;
  
  1)sed 's/this/That/g;s/line/Line/g' /etc/testdir/sed.txt
   
   Note:两个命令都要用一对单引号引起来,最终的目的文件放在最后
   
   That is Line1, That is the First Line
   That is Line2, the second Line, Empty Line followed

   That is Line4, That is Third Line
   That is Line5, That is Fifth Line
   
  2)sed -e 's/this/That/g' -e's/line/Line/g' /etc/testdir/sed.txt
  
   Note:如果使用-e连接多个COMMAND时,每一个命令需要用一对单独的单引号引起来,其显示的结果与上面1)显示的一样
   
 2、使用d命令进行删除指定的行
  使用方法:sed '#d'/etc/testdir/sed.txt  #其中#表示行数的index
  
  1)删除第1行
   Eg: sed '1d'/etc/testdir/sed.txt
    thisis line 2, the second line, Empty line followed

    thisis line 4, this is Third line
    thisis line 5, this is Fifth line
  2)删除第1行后保存到指定的文件中去,
   Eg:sed '1d'/etc/testdir/sed.txt > /etc/testdir/save_sed.txt
   Note:需要使用重定向
   
  3)删除第2行后保存到指定的文件中,并且显示出来
   Eg:[ if `sed'2d' /etc/testdir/sed.txt > /etc/testdir/save_sed.txt `]&& cat /etc/testdir/save_sed.txt
   this is line1, this is the First line

   this is line4, this is Third line
   this is line5, this is Fifth line.
  
  4)sed默认的是不对源文件进行任何修改的,但是如果真想修改怎么办?就需要采用-i选项,下面进行测试
   Eg;首先对其进行复制,因为后面还需要使用,然后直接使用sed命令进行对文件内容进行修改
    cp/etc/testdir/sed.txt /etc/testdir/sed_1.txt
    sed-i '1d' /etc/testdir/sed_1.txt
    cat/etc/testdir/sed_1.txt
    
    thisis line 2, the second line, Empty line followed

    thisis line 4, this is Third line
    thisis line 5, this is Fifth line
    
  5)删除指定的范围的行
   
   Eg:删除sed.txt文件中第1行到2行,并且显示出来
    sed'1,3d' /etc/testdir/sed.txt
    thisis line 4, this is Third line
    thisis line 5, this is Fifth line
   NOTE:
    删除第一行到最后一行是'1,$d'
    删除最后一行是'$d'
    删除指定范围以外的行是'5!d',意思就是只保留第5行
    只保留某一范围行数的是'1,3!d',其实就是删除除了1-3行之外的所有行
    删除包含某个字符的行
     sed'/line/d' /etc/testdir/sed.txt
     
     thisis line 1, this is the First line

     thisis line 4, this is Third line
     thisis line 5, this is Fifth line
    删除空行
     sed'/^$/d' /etc/testdir/sed.txt
     thisis line 1, this is the First line
     thisis line 2, the second line, Empty line followed
     thisis line 4, this is Third line
     thisis line 5, this is Fifth line/
 
 3、如何使用sed命令进行替换,前面1中已经讲到了一部分
  因为前面讲到的是对全局的进行替换,如果没有这样命令存在的话默认是只匹配一个
  Eg:sed 's/line/Line'/etc/testdir/sed.txt
   this is Line1, this is the First line
   this is Line2, the second line, Empty line followed

   this is Line4, this is Third line
   this is Line5, this is Fifth line
  Eg: sed 's/line/Line/'/etc/testdir/sed.txt
   this is line1, this is the First Line
   this is line2, the second Line, Empty line followed

   this is line4, this is Third Line
   this is line5, this is Fifth Line
   
  NOTE:使用方法是sed 's/line/Line/N'/PATH/TO/FILE #N表示index,意思是匹配N个为止,如果是全局则采用g(global)
  
  如何使用sed命令只替换开头的位置的呢?
   sed's/^this/This/g' /etc/testdir/sed.txt
   This is line1, this is the First line
   This is line2, the second line, Empty line followed

   This is line4, this is Third line
   This is line5, this is Fifth line
   
 4、如何使用sed命令进行多个单字符的替换呢
  这里使用的是sed 'y/123/ABC'/etc/testdir/sed.txt #意思就是分别吧1替换成A,2替换成B,3替换成C
   Eg:sed'y/123/ABC/' /etc/testdir/sed.txt
    thisis line A, this is the First line
    thisis line B, the second line, Empty line followed

    thisis line 4, this is Third line
    thisis line 5, this is Fifth line
    
 5、如何使用sed命令进行插入文本的操作
  这里使用的命令是i或者a,其中i表示在匹配到行的前一行插入,而a则表示在匹配到行的后一行插入
  1)使用行的index执行插入命令
  例如:sed 'i Interrt'/etc/testdir/sed.txt #就是在每一行前面都插入一个Insert
    sed 'a Interrt' /etc/testdir/sed.txt #就是在每一行后面都插入一个Insert
    sed 'NUM i insert' /etc/testdir/sed.txt #表示在第NUM行前面插入一个Insert
    sed 'NUM a insert' /etc/testdir/sed.txt #表示在第NUM行后面插入一个Insert
    
  2)在匹配到行为标准进行插入操作
  例如:sed '/second/i\insert'/etc/testdir/sed.txt#意思就是找到可以匹配到second的行,然后在该行的前面一行插入一个Insert
 6、怎样实现从其他文件种读取文本,并进行其他操作呢
  这里主要用的命令是r,顾名思义就是read
  1)实现从别的文件中读取文本,然后进行插入到空白行的后面
   Eg:sed '/^$/r/ect/testdir/' /etc/testdir/sed.txt#其中^$就是匹配到空行,然后在空行后面读入其它文件的内容
   this is line1, this is the First line
   this is line2, the second line, Empty line followed

   My name isTom,
   I am ten yearold
   and,how  are you ?
   hey, what isup?
   this is line4, this   is Third line
   this is line5, this is Fifth line
  2)实现从别的文件中读取文本,然后进行插入到含有特定字符“Empty”行的后面
   Eg:sed'/Empty/r /etc/testdir/sed2.txt' /etc/testdir/sed.txt#找到含有Empty的行,然后在该行的后面读入其相关的内容
   this is line1, this is the First line
   this is line2, the second line, Empty line followed
   My name isTom,
   I am ten yearold
   and,how  are you ?
   hey, what isup?

   this is line4, this is Third line
   is line 5,this is Fifth line
 7、打印,怎样实现打印指定行呢?
  使用 sed -n 'Np'/etc/testdir/sed.txt#注意,其中N表示行index,表示只打印第N行到屏幕,其中必须使用-n选项才能实现指定行的打印
   1)Eg1:打印第1行到屏幕
     sed-n '1p' /etc/testdir/sed.txt
     thisis line 1, this is the First line
   2)Eg2:打印第3行到屏幕
     sed-n '4p' /etc/testdir/sed.txt
     sed-n '4p'/etc/testdir/sed.txt     
   3)Eg3:打印只经过处理的行
     sed-n 's/line/Line/p' /etc/testdir/sed.txt
     thisis Line 1, this is the First line
     thisis Line 2, the second line, Empty line followed
     thisis Line 4, this is Third line
     thisis Line 5, this is Fifth line
   4)Eg4:打印只经过处理的行,并且保存到其他文件
     sed-n 's/line/Line/p' /etc/testdir/sed.txt >/etc/testdir/sed_1.txt
     cat/etc/testdir/sed_1.txt
     thisis Line 1, this is the First line
     thisis Line 2, the second line, Empty line followed
     thisis Line 4, this is Third line
     thisis Line 5, this is Fifth line
  8、怎样把sed处理后的数据流保存到其他文件中去
   方法1:使用-i完成自身修改:sed-i 's/line/Line/g' /etc/testdir/sed.txt
   方法2:使用重定向:sed's/line/Line/g' /etc/testdir/sed.txt >/etc/testdir/save_sed
   方法3:使用w命令,w顾名思义就是write的意思,下面主要介绍w的使用
    sed -n '1,2 w FILENAME' /etc/testdir/sed.txt
    Eg1:把sed.txt文本中的第1行和第2行写到output文件中去
     sed-n '1,2 w /etc/testdir/output' /etc/testdir/sed.txt
     cat/etc/testdir/output
     thisis line 1, this is the First line
     thisis line 2, the second line, Empty line followed
     
  9、使用sed做一个替换的脚本
    vim/etc/testdir/sed.tihuan
    输入:s/this/This/g,然后保存
    使用sed-f /etc/testdir/sed.tihuan /etc/testdir/sed.txt
    结果是:
    Thisis line 1, This is the First line
    Thisis line 2, the second line, Empty line followed

    Thisis line 4, This is Third line
    Thisis line 5, This is Fifth line
  10、一些常见的高级替换
    1)替换匹配行的下一行
     例如:需要经空白行下一行的line替换成Line
     Eg:sed'/^$/{n;s/line/Line/g}' /etc/testdir/sed.txt
     thisis line 1, this is the First line
     thisis line 2, the second line, Empty line followed

     thisis Line 4, this is Third Line
     thisis line 5, this is Fifth line
    2)待补充

    
 
     
  
   

   
   
   
   
   

  
    


  
  
Script: /etc/testdir/sed2.txt
=================script start==================
1 My name is Tom,
2 I am ten year old
3 and, how  are you ?
4 hey, what is up?
==================scriptend===================  


Script: /etc/testdir/sed.txt 
=================script start==================
  1 this is line 1, this is the First line
  2 this is line 2, the second line, Empty linefollowed
  3
  4 this is line 4, this is Third line
  5 this is line 5, this is Fifth line
 
==================script end===================

 

学习使用sed

原创粉丝点击