sed用法详解

1. sed简介

         sed是streameditor的简称,主要用来对文本的行进行处理,功能类似于awk,可以完美配合正则表达式,只是其更加简单,对文本的列处理功能较弱。处理文本时,sed将文件的一行存储在叫“模式空间”的一个缓冲区,处理完时,就默认将缓冲区的文本送至屏幕,接着处理下一行文本,直至将整个文件处理完。

2. sed用法与参数:

sed用法: sed 选项 ‘commond’ inputfile

 

选项:

-e <script> 以-e指定的script来处理输入的文本,使用多个-e可以实现多点编辑

-f<script文件>以选项指定的文件来处理输入的文件

-n仅显示script处理后的结果

-r 支持使用扩展正则表达式

-I 远处编辑


命令(分为三种命令,地址定界,编辑命令,替换标记命令):

(1)地址定界:

不给出地址:对全文进行处理

单个地址:#指定的行进行处理

         /pattern/:被模式匹配的特定行进行处理

地址范围:对给定地址范围的进行处理

      #,# 对给定的特定行号之间的文本进行处理

      #,+# 对给定的行号和其行号的偏移进行处理

      /pat1/,/pat2/对给定的两个模式之间的文本进行处理

     #,/pat1/对给定的行号和第一次模式匹配到的文本进行处理

 步进范围:所谓步进范围就是类似于奇数行或者偶数行

                     1~2奇数行

                     2~2偶数行

(2) 编辑命令:

       d 删除模式空间匹配的行

       p 打印模式空间的行

       a \text 在行后面追加文本

        i \text 在行后面插入文本

       c \text 替换单行或者多行文本

      w /path 保存模式匹配至path指定的文件

      r /path 读path指定的文件至模式空间

     = 为模式空间的行打印行号、

    !为模式空间的行做取反操作

(3) 替换标记命令

        s/// 查找替换分隔符可以使用@#等不常用的字符

         g:行内全局替换

         p:显示替换成功的行

        w  /path 将替换成功的行保存至文件

        & :已匹配字符串标记

  附:sed元字符集:

    ^ 匹配行开始,如:/^sed/匹配所有以sed开头的行。

    $ 匹配行结束,如:/sed$/匹配所有以sed结尾的行。

    . 匹配一个非换行符的任意字符,如:/s.d/匹配s后接一个任意字符,最后是d。

    * 匹配0个或多个字符,如:/*sed/匹配所有模板是一个或多个空格后紧跟sed的行。

    [] 匹配一个指定范围内的字符,如/[Ss]ed/匹配sed和Sed。

    [^] 匹配一个不在指定范围内的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一个字母开头,紧跟ed的行。

    \(..\) 匹配子串,保存匹配的字符,如s/\(love\)able/\1rs,loveable被替换成lovers。

    & 保存搜索字符用来替换其他字符,如s/love/**&**/,love这成**love**。

    \< 匹配单词的开始,如:/\ 匹配单词的结束,如/love\>/匹配包含以love结尾的单词的行。

    x\{m\} 重复字符x,m次,如:/0\{5\}/匹配包含5个0的行。

    x\{m,\} 重复字符x,至少m次,如:/0\{5,\}/匹配至少有5个0的行。

    x\{m,n\} 重复字符x,至少m次,不多于n次,如:/0\{5,10\}/匹配5~10个0的行。


3. sed用法示例:

(1) [root@centos7testdir]# sed '2p' info.txt  //打印第二行,其他行默认打印
NOTE: If you are ever unsure about something you typed, press<ESC> to place
   you in Normal mode.  Then retype the command you wanted.
   you in Normal mode.  Then retype the command you wanted.
(2) [root@centos7testdir]# sed -n '2p' info.txt  // 静默模式,仅打印第二行
     you in Normal mode.  Then retype the command you wanted.
(3)[root@centos7testdir]# sed -n '1,4p' info.txt //打印1-4行
NOTE: If you are ever unsure about something you typed, press<ESC> to place
      you in Normal mode.  Then retype the command you wanted.
 
NOTE: The cursor keys should also work.  But using hjkl you will be able to
(4) [root@centos7testdir]# sed -n '1,/command/p' info.txt  //打印1到模式匹配到的行
NOTE: If you are ever unsure about something you typed, press<ESC> to place
you in Normal mode.  Then retype the command you wanted.
(5)[root@centos7testdir]# sed -n '/^$/=' info.txt  //显示空行的行号
3
[root@centos7 testdir]# sed -n -e '/command/{p;=}' -e '/NOTE/p'  info.txt //多点编辑,打印行号
NOTE: If you are ever unsure about something you typed, press<ESC> to place
      you in Normal mode.  Then retype the command you wanted.
2
NOTE: The cursor keys should also work.  But using hjkl you will be able to
(6)[root@centos7testdir]# sed -n  '/command/i\hello_world'   info.txt
hello_world  //将匹配到的command之后行插入hello_world
(7)[root@centos7testdir]# sed -e '/^$/d' -e '/^[[:space:]]\+$/d' info.txt  //删除空白行或包含空格tab的,
NOTE: If you are ever unsure about something you typed, press<ESC> to place
      you in Normal mode.  Then retype the command you wanted.
NOTE: The cursor keys should also work.  But using hjkl you will be able to
      move around much faster,once you get used to it.  Really!
(8)[root@centos7testdir]# sed -n '1,3 s/you/&ANDME/p' info.txt  /指定范围内的替换
NOTE: If youANDME are ever unsure about something you typed, press<ESC> to place
      youANDME in Normalmode.  Then retype the command youwanted.
(9)[root@centos7testdir]# sed -n '1,4p' info.txt.bak |sed -n -i.backup 's/to/TO/'  info.txt.bak //把1到4行提取出来,并把to该为TO,i修改源文件,并将源文件备份为.backup
(10)[root@centos7testdir]# sed -n  's@^[[:space:]]\+@@p'/etc/grub2.cfg //替换开头多个空格为空
load_env
set default="${next_entry}"
set next_entry=
save_env next_entry
set boot_once=true
…

 

(11)删除fs.txt以#开头,后面至少有一个空白字符的#和空格
[root@centos7 testdir]# head -n5 fs.txt
 
#
# /etc/fstab
# Created by anaconda on Thu Jul 21 11:45:45 2016
#
[root@centos7 testdir]# sed -n 's@#[ ]\+\(.*\)@\1@p' fs.txt
/etc/fstab
Created by anaconda on Thu Jul 21 11:45:45 2016
Accessible filesystems, by reference, are maintained under'/dev/disk'
See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for moreinfo
(12)求目录名:
[root@centos7 testdir]#echo "/etc/fst/sb/" | sed -r's@(.*/)([^/]+/?)$@\1@p'
求基名:
[root@centos7 testdir]#echo "/etc/fst/sb/" | sed -r's@(.*/)([^/]+/?)$@\2@p'



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章