歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> sed 命令詳解

sed 命令詳解

日期:2017/2/28 13:56:36   编辑:Linux教程

概述

sed是stream editor的簡稱,也就是流編輯器。它一次處理一行內容,處理時,把當前處理的行存儲在臨時緩沖區中,稱為“模式空間”(pattern space),接著用sed命令處理緩沖區中的內容,處理完成後,把緩沖區的內容送往屏幕。接著處理下一行,這樣不斷重復,直到文件末尾。文件內容並沒有 改變,除非你使用重定向存儲輸出。

使用語法

sed命令的使用規則是這樣的:

  1. sed[option]'command' input_file

其中option是可選的,常用的option有如下幾種:

  • -n 使用安靜(silent)模式(想不通為什麼不是-s)。在一般sed的用法中,所有來自stdin的內容一般都會被列出到屏幕上。但如果加上-n參數後,則只有經過sed特殊處理的那一行(或者動作)才會被列出來;
  • -e 直接在指令列模式上進行 sed 的動作編輯;
  • -f 直接將 sed 的動作寫在一個文件內, -f filename 則可以執行filename內的sed命令;
  • -r 讓sed命令支持擴展的正則表達式(默認是基礎正則表達式);
  • -i 直接修改讀取的文件內容,而不是由屏幕輸出。

常用的命令有以下幾種:

  • a \:追加行(append), a \的後面跟上字符串s(多行字符串可以用\n分隔),則會在當前選擇的行的後面都加上字符串s;

  • c \:取代/替換行(change),c \後面跟上字符串s(多行字符串可以用\n分隔),則會將當前選中的行替換成字符串s;
  • i \:插入行(insert),i \後面跟上字符串s(多行字符串可以用\n分隔),則會在當前選中的行的前面都插入字符串s;
  • d:刪除行(delete),該命令會將當前選中的行刪除;
  • p:打印print,該命令會打印當前選擇的行到屏幕上;
  • s:替換字符串(subs),通常s命令的用法是這樣的:1,2s/old/new/g,將old字符串替換成new字符串

命令示例

假設有一個本地文件test.txt,文件內容如下:

  1. [qifuguang@winwill~]$ cattest.txt
  2. thisis first line
  3. thisis second line
  4. thisis third line
  5. thisis fourth line
  6. this fifth line
  7. happy everyday
  8. end

本節將使用該文件詳細演示每一個命令的用法。

a命令(追加行)

例一

  1. [qifuguang@winwill~]$ sed'1a \add one'test.txt
  2. thisis first line
  3. add one
  4. thisis second line
  5. thisis third line
  6. thisis fourth line
  7. thisis fifth line
  8. happy everyday
  9. end

本例命令部分中的1表示第一行,同樣的第二行寫成2,第一行到第三行寫成1,3,用$表示最後一行,比如2,$表示第二行到最後一行中間所有的行(包含第二行和最後一行)。

本例的作用是在第一行之後增加字符串”add one”,從輸出可以看到具體效果。

例二

  1. [qifuguang@winwill~]$ sed'1,$a \add one'test.txt
  2. thisis first line
  3. add one
  4. thisis second line
  5. add one
  6. thisis third line
  7. add one
  8. thisis fourth line
  9. add one
  10. thisis fifth line
  11. add one
  12. happy everyday
  13. add one
  14. end
  15. add one

本例表示在第一行和最後一行所有的行後面都加上”add one”字符串,從輸出可以看到效果。

例三

  1. [qifuguang@winwill~]$ sed'/first/a \add one'test.txt
  2. thisis first line
  3. add one
  4. thisis second line
  5. thisis third line
  6. thisis fourth line
  7. thisis fifth line
  8. happy everyday
  9. end

本例表示在包含”first”字符串的行的後面加上字符串”add one”,從輸出可以看到第一行包含first,所以第一行之後增加了”add one”

例四

  1. [qifuguang@winwill~]$ sed'/^ha.*day$/a \add one'test.txt
  2. thisis first line
  3. thisis second line
  4. thisis third line
  5. thisis fourth line
  6. thisis fifth line
  7. happy everyday
  8. add one
  9. end

本例使用正則表達式匹配行,^ha.*day$表示以ha開頭,以day結尾的行,則可以匹配到文件的”happy everyday”這樣,所以在該行後面增加了”add one”字符串。

i命令(插入行)

i命令使用方法和a命令一樣的,只不過是在匹配的行的前面插入字符串,所以直接將上面a命令的示例的a替換成i即可,在此就不啰嗦了。

c命令(替換行)

例五

  1. [qifuguang@winwill~]$ sed'$c \add one'test.txt
  2. thisis first line
  3. thisis second line
  4. thisis third line
  5. thisis fourth line
  6. thisis fifth line
  7. happy everyday
  8. add one

本例表示將最後一行替換成字符串”add one”,從輸出可以看到效果。

例六

  1. [qifuguang@winwill~]$ sed'4,$c \add one'test.txt
  2. thisis first line
  3. thisis second line
  4. thisis third line
  5. add one

本例將第四行到最後一行的內容替換成字符串”add one”。

例七

  1. [qifuguang@winwill~]$ sed'/^ha.*day$/c \replace line'test.txt
  2. thisis first line
  3. thisis second line
  4. thisis third line
  5. thisis fourth line
  6. thisis fifth line
  7. replace line
  8. end

本例將以ha開頭,以day結尾的行替換成”replace line”。

d命令(刪除行)

例八

  1. [qifuguang@winwill~]$ sed'/^ha.*day$/d'test.txt
  2. thisis first line
  3. thisis second line
  4. thisis third line
  5. thisis fourth line
  6. thisis fifth line
  7. end

本例刪除以ha開頭,以day結尾的行。

例九

  1. [qifuguang@winwill~]$ sed'4,$d'test.txt
  2. thisis first line
  3. thisis second line
  4. thisis third line

本例刪除第四行到最後一行中的內容。

p命令(打印行)

例十

  1. [qifuguang@winwill~]$ sed-n '4,$p'test.txt
  2. thisis fourth line
  3. thisis fifth line
  4. happy everyday
  5. end

本例在屏幕上打印第四行到最後一行的內容,p命令一般和-n選項一起使用。

例十一

  1. [qifuguang@winwill~]$ sed-n '/^ha.*day$/p'test.txt
  2. happy everyday

本例打印以ha開始,以day結尾的行。

s命令(替換字符串)

實際運用中s命令式最常使用到的。

例十二

  1. [qifuguang@winwill~]$ sed's/line/text/g'test.txt
  2. thisis first text
  3. thisis second text
  4. thisis third text
  5. thisis fourth text
  6. thisis fifth text
  7. happy everyday
  8. end

本例將文件中的所有line替換成text,最後的g是global的意思,也就是全局替換,如果不加g,則只會替換本行的第一個line。

例十三

  1. [qifuguang@winwill~]$ sed'/^ha.*day$/s/happy/very happy/g'test.txt
  2. thisis first line
  3. thisis second line
  4. thisis third line
  5. thisis fourth line
  6. thisis fifth line
  7. very happy everyday
  8. end

本例首先匹配以ha開始,以day結尾的行,本例中匹配到的行是”happy everyday”這樣,然後再將該行中的happy替換成very happy。

例十四

  1. [qifuguang@winwill~]$ sed's/\(.*\)line$/\1/g'test.txt
  2. thisis first
  3. thisis second
  4. thisis third
  5. thisis fourth
  6. thisis fifth
  7. happy everyday
  8. end

這個例子有點復雜,先分解一下。首先s命令的模式是s/old/new/g這樣的,所以本例的old部分即\(.*\)line$,sed命令中使用\(\)包裹的內容表示正則表達式的第n部分,序號從1開始計算,本例中只有一個\(\)所以\(.*\)表示正則表達式的第一部分,這部分匹配任意字符串,所以\(.*\)line$匹配的就是以line結尾的任何行。然後將匹配到的行替換成正則表達式的第一部分(本例中相當於刪除line部分),使用\1表示匹配到的第一部分,同樣\2表示第二部分,\3表示第三部分,可以依次這樣引用。比如下面的例子:

  1. [qifuguang@winwill~]$ sed's/\(.*\)is\(.*\)line/\1\2/g'test.txt
  2. this first
  3. this second
  4. this third
  5. this fourth
  6. this fifth
  7. happy everyday
  8. end

正則表達式中is兩邊的部分可以用\1\2表示,該例子的作用其實就是刪除中間部分的is。

在sed命令中引入shell變量 http://www.linuxidc.com/Linux/2014-03/97896.htm

Linux下Shell編程——sed命令基本用法 http://www.linuxidc.com/Linux/2013-06/85526.htm

Unix文本處理工具之sed http://www.linuxidc.com/Linux/2013-08/89315.htm

sed 高級用法 http://www.linuxidc.com/Linux/2014-09/106961.htm

sed命令詳解與示例 http://www.linuxidc.com/Linux/2014-11/109325.htm

Linux正則表達式sed 詳述 http://www.linuxidc.com/Linux/2015-04/116309.htm

Linux文本處理工具之sed http://www.linuxidc.com/Linux/2015-01/111436.htm

Copyright © Linux教程網 All Rights Reserved