ed命令簡介

http://bbs.chinaunix.net/thread-1262996-1-1.html

  1. ed 命令簡介:

  2. 在 unix/linux 平臺下,首先要學習的就是文本編輯,而 ed 編輯器是 unix/linux 操作系統下最簡單、最典型
  3. 的文本編輯器,因此,學習 ed 編輯器能夠幫助你很好的理解一般的文本操作,而且還可以爲你以後學習 unix/linux
  4. 下的一些常用命令(如 grep,sed,...)打下基礎。 

  5. ed 編輯器是以行爲單位對文本進行操作的編輯器,也就是說一次僅能編輯一行,非以全屏的方式來操作,而不像 vi/vim
  6. 那樣是以整個屏幕框架爲單位對文本進行編輯。ed 簡單、易用,也正是由於其小巧玲瓏,許多文本操作者現在仍然偏愛它,
  7. 使用它。

  8. ed 編輯器可以用於創建、修改、顯示文本文件。當 ed 打開一文本文件時,將複製文件的內容到 ed 命令的緩存中, 在
  9. ed 中的所有命令操作只作用於緩存中的內容,而不會對源文件有任何影響,除非對源文件進行 w(寫) 操作。 ed 有命令模式
  10. 和輸入模式兩種狀態,類似於 vi/vim 的兩種模式,ed 在命令模式下輸入的是命令,這些命令用來指定對編輯文本的操作;
  11. ed 在輸入模式下輸入的是文本,這些文本將依照命令模式下輸入的命令被添加或替換到文本中。當第一次被調用時,ed 默
  12. 認進入命令模式,當輸入 a、c 或 i 命令後即進入輸入模式,在輸入模式下,單行輸入 '.' 然後回車,則回到命令模式。


  13. ed 調用:

  14. ed [-GVhl] [-p string] [file]

  15. 註解:
  16. file                指定將要編輯的文件(如果文件名以歎號開頭 '!file',應用反斜槓引用歎號 '\!file',否則,ed 將視 file 
  17.                 命令的輸出爲編輯內容。)
  18. -G                兼容模式
  19. -V                輸出 ed 版本信息                
  20. -h                顯示 ed 幫助信息
  21. -l                強制退出狀態爲 0(即使操作中出現錯誤)
  22. -p string         指定命令模式下的提示符


  23. ed 命令格式爲:

  24. [address [,address]]command[parameters]

  25. 地址(address,[,address]) 是用來指定命令(command)的作用範圍的。如果沒給出地址,則爲默認地址。ed 讀入文件後,當前
  26. 行默認爲文件最後一行,如果文件爲空,則當前行號設置爲 0。

  27. 命令(command) 會根據指定的地址(或默認地址)對編輯文本進行操作。當輸入一非法命令,ed 將會顯示一問號 '?' 來提示
  28. 輸入命令錯誤。


  29. ed 地址:

  30. 地址由下面基本的元素構成,其後也可跟加 '+'、'-'、' ' 和數字來表示自己所需要的地址。

  31. .             當前行

  32. $             文本最後一行

  33. n             文本第 n 行( n 爲數字,下同;m 亦是 )

  34. -n             從文本當前行數起,向前第 n 行

  35. +n             從文本當前行數起,向後第 n 行

  36. -             相當於 -1 行地址

  37. +             相當於 +1 行地址

  38. m,n             文本的第 m 到 n 行

  39. ,             文本的所有行

  40. ;             文本當前行到最後一行

  41. /reg/             從文本當前行數起,下一個匹配 reg 的行

  42. ?reg?             從文本當前行數起,上一個匹配 reg 的行

  43. 'x             由 k 命令標記的行( x 爲一小寫字母 )


  44. 正則表達式如下:(只涉及 ed 所支持的正則表達式)

  45. .                   匹配任何單個字符。

  46. [char-class]           匹配任何一個在 char-class 裏的單個字符。如果中間出現 '-' ,則意爲其左邊的字符和其右邊的
  47.                    字符之間的所有字符。例如,[abc] 匹配 a 或 b 或 c;[a-z] 匹配任意一個小寫字母(a、b、c、
  48.                     ...、z),[0-9] 匹配任意一個數字(0、1、2、...、9)。
  49.                     char-class 也可以爲一些字符集。如下:
  50.                    [:alpha:] 相當於 [a-zA-Z]
  51.                    [:lower:] 相當於 [a-z]
  52.                    [:upper:] 相當於 [A-Z]
  53.                    [:digit:] 相當於 [0-9]
  54.                    [:alnum:] 相當於 [a-zA-Z0-9]
  55.                    [:blank:] 匹配 ' '(空格)、 '\t'(製表符)
  56.                    [:space:] 匹配 ' '(空格)、'\t'(製表符)、'\n'(新行)、'\f'()、'\v'(垂直製表符)、'\r'(回車符)
  57.                    [:cntrl:] 匹配控制字符。在 ASCII 碼中,這些控制字符是從八進制數字 000 到 037, 和 177 (DEL) 
  58.                    [:print:] 匹配 相當於 [:alnum:]、[:punct:] 和 空格
  59.                    [:graph:] 匹配 相當於 [:alnum:] 、 [:punct:]
  60.                    [:punct:] 匹配 `! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~ ' 等標點符號
  61.                    [:xdigit:] 匹配十六進制字符 '0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f'

  62. [^char-class]            匹配 [char-class] 的補集,即匹配任意一個不在 char-class 裏的單個字符

  63. *                    匹配前面的子表達式零次或多次。例如,'ab*' 能匹配 "a" 以及 "abb"。 * 等價於'\{0,\}'

  64. \+                   匹配前面的子表達式一次或多次。例如,'ab\+' 能匹配 "ab" 以及 "abb",但不能匹配 "a"。\+ 等
  65.                    價於 \{1,\}

  66. \?                   匹配前面的子表達式零次或一次。例如,"word(s)\?" 可以匹配 "word" 或 "words" 。\? 等價於
  67.                      \{0,1\}

  68. \{n,m\}           m 和 n 均爲非負整數,其中n <= m。最少匹配 n 次且最多匹配 m 次。 "a{1,3}" 將匹配 "baaaaab" 
  69.                    中的前三個 a。'a{0,1}' 等價於 'a\?'。請注意在逗號和兩個數之間不能有空格

  70. \{n,\}                   n 是一個非負整數。至少匹配n 次。例如,'a\{2,\}' 不能匹配 "abc" 中的 'a',但能匹配 
  71.                   "baaaaab"中的所有 a。'a\{1,\}' 等價於 'a\+'。'a\{0,\}' 則等價於 'a*'

  72. \{n\}                   n 是一個非負整數。匹配確定的 n 次。例如,'a\{2\}' 不能匹配 "bab" 中的 'a',但是能匹配 
  73.                    "baab"中的兩個 a

  74. ^                   匹配輸入字符串的開始位置

  75. $                   匹配輸入字符串的結束位置

  76. \<                   匹配一個單詞的前邊界。例如,'\<el' 匹配 "element",但不能匹配 "help"

  77. \>                   匹配一個單詞的後邊界。例如,'ly\>' 匹配 "lovely",但不匹配 "lying"

  78. \b                   匹配一個單詞邊界,也就是指單詞和空格間的位置。例如, 'er\b' 可以匹配"never" 中的 'er',
  79.                    但不能匹配"verb" 中的 'er'

  80. \B                   匹配非單詞邊界。'er\B' 能匹配 "verb" 中的 'er',但不能匹配 "never" 中的 'er'

  81. \w                   匹配任何非單詞字符。等價於 '[^A-Za-z0-9_]'

  82. \W                   匹配任何非單詞字符。等價於 '[^A-Za-z0-9_]'

  83. \`                   匹配一個句子的邊界

  84. \(\)                   定義向後引用。'\n'(n爲一正整數)代表第 n 個括號中匹配的字符串


  85. ed 命令:
  86. ed 命令都是單個字符,其中一些命令有一些選項。如果一命令超過一行,應使 '\' 結束每一行。
  87. 命令如下:(括號內爲默認地址)

  88. (.)a            切換到輸入模式,將新輸入的文本追加到指定行的後面,當前行被設爲輸入文本的最後一行

  89. (.)i            切換到輸入模式,將新輸入的文本插入到指定行的前面,當前行被設爲輸入文本的最後一行

  90. (.,.)c          切換到輸入模式,將新輸入的文本替換成指定行,當前行被設爲輸入文本的最後一行

  91. (.,.)d          刪除指定行,如果被刪除的文本後還有文本行,則當前行被設爲該行,否則設爲被刪除的文本的
  92.                 上一行

  93. (.+1)          無命令時,默認 p 命令,但打印下一行內容,當前行被設爲打印行

  94. (.+1)zn        一次跳動 n 行,如果未指出 n ,默認當前終端屏幕大小,當前行被設爲最後被打印的行

  95. (.,.)p         打印指定行,當前行被設爲打印行的最後一行

  96. P               ed 命令模式下提示符開關命令,默認提示符爲 '*'

  97. (.,.)l          在每行最後加一 '$' 符號指定結尾,並打印輸出

  98. (.,.)n         打印指定行號和內容,行號與行內容用製表符分割,當前行被設爲打印行的最後一行

  99. ($)=           打印指定行行號

  100. (.,.)#         註釋行,將被忽略

  101. (.)k char      用一小寫字母標記指定行

  102. (.,.)s/reg/replacement/
  103. (.,.)s/reg/replacement/g
  104. (.,.)s/reg/replacement/n
  105.                               替換指定行命令

  106. (.,.)s                   重複上一次替換命令,當前行被設爲最後一個被改變的行

  107. (1,$)g/reg/cmd-list     所有匹配 '/reg/' 的行執行 cmd-list 命令,在命令執行前,當前行被設爲匹配行。當所有匹配
  108.                          行執行完命令後,當前行被設定爲最後一個匹配行。cmd-list 中每一行只能有一個命令,但有多
  109.                          個命令時,應以 '\' 結束每一行

  110. (1,$)G/reg/              與 g/reg/cmd-list 相似,但匹配的每一行所執行的命令由用戶各個定義。

  111. (1,$)v/reg/cmd-list      與 g/reg/cmd-list 相反,指不匹配行

  112. (1,$)V/reg/              與 G/reg/ 相反,指不匹配行

  113. (.,.+1)j                          合併指定行內容,當前行被設爲合併行

  114. (.,.)m(.)                移動左邊源指定行到右邊目的指定行後,當前行被設爲移動行的最後一行

  115. (.,.)t(.)                 複製左邊源指定行到右邊目的指定行後,當前行被設爲複製行的最後一行

  116. (.,.)y                   複製指定行到緩存,當前行不改變

  117. (.)x                     複製緩存內容到指定行後,當前行被設爲複製行的最後一行

  118. u                        撤銷上一次命令,當前地址被設爲上一次地址

  119. h                        打印最後一個錯誤說明

  120. H                        錯誤說明開關,默認不輸出

  121. e file                    編輯文件並設定文件名

  122. E file                    強制編輯文件,同 e file,但丟失以前的修改,不做警告

  123. !cmd                    執行 shell 命令 cmd

  124. e !cmd                  先將ed 緩衝區清除,替換 cmd 命令的輸出

  125. f file                    設置文件名,如果每給出 file 參數,則打印文件名

  126. ($)r file                 把指定文件內容追加到指定行後,當前行被設爲追加文本的最後一行

  127. ($)r !cmd               把命令的輸出追加到指定行後,當前行被設爲追加文本的最後一行

  128. (1,$)w file               保存指定文本內容到指定文件(覆蓋保存)

  129. (1,$)W file              保存指定文本內容到指定文件(追加保存),當前行不改變

  130. (1,$)w !cmd            輸出指定文本內容到 cmd 的標準輸入,當前行不改變

  131. (1,$)wq flie             保存指定文本內容到指定文件(覆蓋保存),並退出編輯器

  132. q                  退出 ed 編輯器,退出前若所作的修改沒保存,發出警告

  133. Q                  強制退出 ed 編輯器,同 q 命令,但退出前若所作的修改沒保存,不警告

  134. P                  ed 命令提示符顯示開關。 '*' 爲 ed 默認提示符,利用 ed 命令 -p 選項,其可被更改爲任意字符

  135. 更爲詳細之處請參閱 man info 手冊
複製代碼


對於新手來說,不結合例子是很難上手的,下面給出一些示例

看示例時,注意命令前後文本的變化,進而理解命令的作用

(不能概括所有的命令,若想了解,請自己動手練習
注意:示例之間有一定上下文聯繫,應從前到後閱讀
               其中 '#' 後的中文部分是解釋、說明 )
  1. $cat a                                #顯示文件 a 的內容
  2. this is the first line
  3. this is the second line
  4. this is the third line
  5. this is the forth line
  6. this is the fifth line
  7. $ed a                                #ed 編輯 a 文件
  8. 116
  9. P                                #顯示默認命令模式提示符 '*'
  10. *,p                                #'*' 爲默認命令模式提示符, ',' 表示文件全部的行,'p'打印命令:該行命令
  11. this is the first line              #即解釋爲“打印文件全部行”
  12. this is the second line
  13. this is the third line
  14. this is the forth line
  15. this is the fifth line
  16. *2p                                #打印第二行
  17. this is the second line
  18. *3-1p                                #打印第二行
  19. this is the second line
  20. *3+1p                                #打印第四行
  21. this is the forth line
  22. *q                                #退出命令
  23. $
複製代碼
  1. $ed                                #調用 ed
  2. a                                #添加文本命令
  3. this is the first line
  4. this is the second line
  5. this is the third line
  6. this is the fourth line
  7. this is the fifth line
  8. .                                #結束添加文本
  9. ,p                                #打印全部編輯文本
  10. this is the first line
  11. this is the second line
  12. this is the third line
  13. this is the fourth line
  14. this is the fifth line
  15. 2                                #打印第二行
  16. this is the second line
  17. n                                #顯示行號與行內容
  18. 2       this is the second line
  19. =                                #顯示文本總行數
  20. 5
  21. .=                                #顯示當前行號
  22. 2
  23. i                                #插入文本命令
  24. this is the insert line
  25. .                                #結束插入文本
  26. n                                #顯示行號與行內容
  27. 2       this is the insert line
  28. ,p                                #打印全部編輯文本
  29. this is the first line
  30. this is the insert line
  31. this is the second line
  32. this is the third line
  33. this is the fourth line
  34. this is the fifth line
  35. c                                #更改當前行
  36. this is the first change line
  37. this is the second change line
  38. .                                #結束插入更改文本
  39. ,p                                #打印全部編輯文本
  40. this is the first line
  41. this is the first changed line
  42. this is the second changed line
  43. this is the second line
  44. this is the third line
  45. this is the fourth line
  46. this is the fifth line
  47. g/changed/n                        #打印含有“changed”的行號和行內容
  48. 2       this is the first change line
  49. 3       this is the second change line
  50. 2,3d                                #將第2到3行刪除
  51. ,p                                #打印全部編輯文本
  52. this is the first line
  53. this is the second line
  54. this is the third line
  55. this is the fourth line
  56. this is the fifth line
  57. wq b                                #保存文本內容到 b 文件並退出
  58. 117
  59. $ls                                #shell 命令:列出當前文件夾下的文件名
  60. a  b
  61. $
複製代碼
  1. $ed b                                #ed 編輯 b 文件
  2. 117
  3. ,p
  4. this is the first line
  5. this is the second line
  6. this is the third line
  7. this is the fourth line
  8. this is the fifth line
  9. 2s/second/2th/
  10. ,p                                #打印全部編輯文本
  11. this is the first line
  12. this is the 2th line
  13. this is the third line
  14. this is the fourth line
  15. this is the fifth line
  16. ,s/this is //                #將所有行中第一個“this is ”替換爲“”,即刪除
  17. ,p                                #打印全部編輯文本
  18. the first line
  19. the 2th line
  20. the third line
  21. the fourth line
  22. the fifth line
  23. wq                                #保存文件並退出
  24. 74
  25. $cat b                                #shell 命令,顯示文件 b
  26. the first line
  27. the 2th line
  28. the third line
  29. the fourth line
  30. the fifth line
  31. $
複製代碼
  1. $ed b                                #ed 編輯 b 文件
  2. 74
  3. ,p                                #打印全部編輯文本
  4. the first line
  5. the 2th line
  6. the third line
  7. the fourth line
  8. the fifth line
  9. 2,4m5                                #將第2到4行文本內容移到第5行後
  10. ,p                                #打印全部編輯文本
  11. the first line
  12. the fifth line
  13. the 2th line
  14. the third line
  15. the fourth line
  16. u                                #撤銷上一次命令
  17. ,p                                #打印全部編輯文本
  18. the first line
  19. the 2th line
  20. the third line
  21. the fourth line
  22. the fifth line
  23. 2t0                                #複製第2行文本到第0行,亦即第一行前
  24. ,p                                #打印全部編輯文本
  25. the 2th line
  26. the first line
  27. the 2th line
  28. the third line
  29. the fourth line
  30. the fifth line
  31. u                                #撤銷上一次命令
  32. n                                #顯示行號與行內容
  33. 5       the fifth line
  34. r !date                        #將 shell 命令 date 的輸出添加到當前行後
  35. 43
  36. ,p                                #打印全部編輯文本
  37. the first line
  38. the 2th line
  39. the third line
  40. the fourth line
  41. the fifth line
  42. 2008年 09月 03日 星期三  20:49:25 CST
  43. r !cal                                #將 shell 命令 cal 的輸出添加到當前行後
  44. 145
  45. ,p                                #打印全部編輯文本
  46. the first line
  47. the 2th line
  48. the third line
  49. the fourth line
  50. the fifth line
  51. 2008年 09月 03日 星期三  20:49:25 CST
  52.        九月  2008
  53. 日 一 二 三 四 五 六
  54.     1  2  3  4  5  6
  55. 7  8  9 10 11 12 13
  56. 14 15 16 17 18 19 20
  57. 21 22 23 24 25 26 27
  58. 28 29 30
  59. q                                #退出 ed
  60. ?                                #“?”提示輸入錯誤,該錯誤爲退出錢沒保存文本
  61. wq                                #保存文本並退出
  62. 262
  63. $
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章