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'



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