ed命令详解

ed 命令简介:

在 unix/linux 平台下,首先要学习的就是文本编辑,而 ed 编辑器是 unix/linux 操作系统下最简单、最典型
的文本编辑器,因此,学习 ed 编辑器能够帮助你很好的理解一般的文本操作,而且还可以为你以后学习 unix/linux
下的一些常用命令(如 grep,sed,...)打下基础。

ed 编辑器是以行为单位对文本进行操作的编辑器,也就是说一次仅能编辑一行,非以全屏的方式来操作,而不像 vi/vim
那样是以整个屏幕框架为单位对文本进行编辑。ed 简单、易用,也正是由于其小巧玲珑,许多文本操作者现在仍然偏爱它,
使用它。

ed 编辑器可以用于创建、修改、显示文本文件。当 ed 打开一文本文件时,将复制文件的内容到 ed 命令的缓存中, 在
ed 中的所有命令操作只作用于缓存中的内容,而不会对源文件有任何影响,除非对源文件进行 w(写) 操作。 ed 有命令模式
和输入模式两种状态,类似于 vi/vim 的两种模式,ed 在命令模式下输入的是命令,这些命令用来指定对编辑文本的操作;
ed 在输入模式下输入的是文本,这些文本将依照命令模式下输入的命令被添加或替换到文本中。当第一次被调用时,ed 默
认进入命令模式,当输入 a、c 或 i 命令后即进入输入模式,在输入模式下,单行输入 '.' 然后回车,则回到命令模式。


ed 调用:

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

注解:
file                指定将要编辑的文件(如果文件名以叹号开头 '!file',应用反斜杠引用叹号 '/!file',否则,ed 将视 file
                命令的输出为编辑内容。)
-G                兼容模式
-V                输出 ed 版本信息               
-h                显示 ed 帮助信息
-l                强制退出状态为 0(即使操作中出现错误)
-p string         指定命令模式下的提示符


ed 命令格式为:

[address [,address]]command[parameters]

地址(address,[,address]) 是用来指定命令(command)的作用范围的。如果没给出地址,则为默认地址。ed 读入文件后,当前
行默认为文件最后一行,如果文件为空,则当前行号设置为 0。

命令(command) 会根据指定的地址(或默认地址)对编辑文本进行操作。当输入一非法命令,ed 将会显示一问号 '?' 来提示
输入命令错误。


ed 地址:

地址由下面基本的元素构成,其后也可跟加 '+'、'-'、' ' 和数字来表示自己所需要的地址。

.             当前行

$             文本最后一行

n             文本第 n 行( n 为数字,下同;m 亦是 )

-n             从文本当前行数起,向前第 n 行

+n             从文本当前行数起,向后第 n 行

-             相当于 -1 行地址

+             相当于 +1 行地址

m,n             文本的第 m 到 n 行

,             文本的所有行

;             文本当前行到最后一行

/reg/             从文本当前行数起,下一个匹配 reg 的行

?reg?             从文本当前行数起,上一个匹配 reg 的行

'x             由 k 命令标记的行( x 为一小写字母 )


正则表达式如下:(只涉及 ed 所支持的正则表达式)

.                   匹配任何单个字符。

[char-class]           匹配任何一个在 char-class 里的单个字符。如果中间出现 '-' ,则意为其左边的字符和其右边的
                   字符之间的所有字符。例如,[abc] 匹配 a 或 b 或 c;[a-z] 匹配任意一个小写字母(a、b、c、
                    ...、z),[0-9] 匹配任意一个数字(0、1、2、...、9)。
                    char-class 也可以为一些字符集。如下:
                   [:alpha:] 相当于 [a-zA-Z]
                   [:lower:] 相当于 [a-z]
                   [:upper:] 相当于 [A-Z]
                   [:digit:] 相当于 [0-9]
                   [:alnum:] 相当于 [a-zA-Z0-9]
                   [:blank:] 匹配 ' '(空格)、 '/t'(制表符)
                   [:space:] 匹配 ' '(空格)、'/t'(制表符)、'/n'(新行)、'/f'()、'/v'(垂直制表符)、'/r'(回车符)
                   [:cntrl:] 匹配控制字符。在 ASCII 码中,这些控制字符是从八进制数字 000 到 037, 和 177 (DEL)
                   [:print:] 匹配 相当于 [:alnum:]、[:punct:] 和 空格
                   [:graph:] 匹配 相当于 [:alnum:] 、 [:punct:]
                   [:punct:] 匹配 `! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ / ] ^ _ ` { | } ~ ' 等标点符号
                   [:xdigit:] 匹配十六进制字符 '0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f'

[^char-class]            匹配 [char-class] 的补集,即匹配任意一个不在 char-class 里的单个字符

*                    匹配前面的子表达式零次或多次。例如,'ab*' 能匹配 "a" 以及 "abb"。 * 等价于'/{0,/}'

/+                   匹配前面的子表达式一次或多次。例如,'ab/+' 能匹配 "ab" 以及 "abb",但不能匹配 "a"。/+ 等
                   价于 /{1,/}

/?                   匹配前面的子表达式零次或一次。例如,"word(s)/?" 可以匹配 "word" 或 "words" 。/? 等价于
                     /{0,1/}

/{n,m/}           m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。 "a{1,3}" 将匹配 "baaaaab"
                   中的前三个 a。'a{0,1}' 等价于 'a/?'。请注意在逗号和两个数之间不能有空格

/{n,/}                   n 是一个非负整数。至少匹配n 次。例如,'a/{2,/}' 不能匹配 "abc" 中的 'a',但能匹配
                  "baaaaab"中的所有 a。'a/{1,/}' 等价于 'a/+'。'a/{0,/}' 则等价于 'a*'

/{n/}                   n 是一个非负整数。匹配确定的 n 次。例如,'a/{2/}' 不能匹配 "bab" 中的 'a',但是能匹配
                   "baab"中的两个 a

^                   匹配输入字符串的开始位置

$                   匹配输入字符串的结束位置

/<                   匹配一个单词的前边界。例如,'/<el' 匹配 "element",但不能匹配 "help"

/>                   匹配一个单词的后边界。例如,'ly/>' 匹配 "lovely",但不匹配 "lying"

/b                   匹配一个单词边界,也就是指单词和空格间的位置。例如, 'er/b' 可以匹配"never" 中的 'er',
                   但不能匹配"verb" 中的 'er'

/B                   匹配非单词边界。'er/B' 能匹配 "verb" 中的 'er',但不能匹配 "never" 中的 'er'

/w                   匹配任何非单词字符。等价于 '[^A-Za-z0-9_]'

/W                   匹配任何非单词字符。等价于 '[^A-Za-z0-9_]'

/`                   匹配一个句子的边界

/(/)                   定义向后引用。'/n'(n为一正整数)代表第 n 个括号中匹配的字符串


ed 命令:
ed 命令都是单个字符,其中一些命令有一些选项。如果一命令超过一行,应使 '/' 结束每一行。
命令如下:(括号内为默认地址)

(.)a            切换到输入模式,将新输入的文本追加到指定行的后面,当前行被设为输入文本的最后一行

(.)i            切换到输入模式,将新输入的文本插入到指定行的前面,当前行被设为输入文本的最后一行

(.,.)c          切换到输入模式,将新输入的文本替换成指定行,当前行被设为输入文本的最后一行

(.,.)d          删除指定行,如果被删除的文本后还有文本行,则当前行被设为该行,否则设为被删除的文本的
                上一行

(.+1)          无命令时,默认 p 命令,但打印下一行内容,当前行被设为打印行

(.+1)zn        一次跳动 n 行,如果未指出 n ,默认当前终端屏幕大小,当前行被设为最后被打印的行

(.,.)p         打印指定行,当前行被设为打印行的最后一行

P               ed 命令模式下提示符开关命令,默认提示符为 '*'

(.,.)l          在每行最后加一 '$' 符号指定结尾,并打印输出

(.,.)n         打印指定行号和内容,行号与行内容用制表符分割,当前行被设为打印行的最后一行

($)=           打印指定行行号

(.,.)#         注释行,将被忽略

(.)k char      用一小写字母标记指定行

(.,.)s/reg/replacement/
(.,.)s/reg/replacement/g
(.,.)s/reg/replacement/n
                              替换指定行命令

(.,.)s                   重复上一次替换命令,当前行被设为最后一个被改变的行

(1,$)g/reg/cmd-list     所有匹配 '/reg/' 的行执行 cmd-list 命令,在命令执行前,当前行被设为匹配行。当所有匹配
                         行执行完命令后,当前行被设定为最后一个匹配行。cmd-list 中每一行只能有一个命令,但有多
                         个命令时,应以 '/' 结束每一行

(1,$)G/reg/              与 g/reg/cmd-list 相似,但匹配的每一行所执行的命令由用户各个定义。

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

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

(.,.+1)j                          合并指定行内容,当前行被设为合并行

(.,.)m(.)                移动左边源指定行到右边目的指定行后,当前行被设为移动行的最后一行

(.,.)t(.)                 复制左边源指定行到右边目的指定行后,当前行被设为复制行的最后一行

(.,.)y                   复制指定行到缓存,当前行不改变

(.)x                     复制缓存内容到指定行后,当前行被设为复制行的最后一行

u                        撤销上一次命令,当前地址被设为上一次地址

h                        打印最后一个错误说明

H                        错误说明开关,默认不输出

e file                    编辑文件并设定文件名

E file                    强制编辑文件,同 e file,但丢失以前的修改,不做警告

!cmd                    执行 shell 命令 cmd

e !cmd                  先将ed 缓冲区清除,替换 cmd 命令的输出

f file                    设置文件名,如果每给出 file 参数,则打印文件名

($)r file                 把指定文件内容追加到指定行后,当前行被设为追加文本的最后一行

($)r !cmd               把命令的输出追加到指定行后,当前行被设为追加文本的最后一行

(1,$)w file               保存指定文本内容到指定文件(覆盖保存)

(1,$)W file              保存指定文本内容到指定文件(追加保存),当前行不改变

(1,$)w !cmd            输出指定文本内容到 cmd 的标准输入,当前行不改变

(1,$)wq flie             保存指定文本内容到指定文件(覆盖保存),并退出编辑器

q                  退出 ed 编辑器,退出前若所作的修改没保存,发出警告

Q                  强制退出 ed 编辑器,同 q 命令,但退出前若所作的修改没保存,不警告

P                  ed 命令提示符显示开关。 '*' 为 ed 默认提示符,利用 ed 命令 -p 选项,其可被更改为任意字符

更为详细之处请参阅 man info 手册

 

---------------------------------------------------------------------------------------------------------------------------------------

 

 

$cat a                                #显示文件 a 的内容
this is the first line
this is the second line
this is the third line
this is the forth line
this is the fifth line
$ed a                                #ed 编辑 a 文件
116
P                                #显示默认命令模式提示符 '*'
*,p                                #'*' 为默认命令模式提示符, ',' 表示文件全部的行,'p'打印命令:该行命令
this is the first line              #即解释为“打印文件全部行”
this is the second line
this is the third line
this is the forth line
this is the fifth line
*2p                                #打印第二行
this is the second line
*3-1p                                #打印第二行
this is the second line
*3+1p                                #打印第四行
this is the forth line
*q                                #退出命令
$

 

 

 

$ed                                #调用 ed
a                                #添加文本命令
this is the first line
this is the second line
this is the third line
this is the fourth line
this is the fifth line
.                                #结束添加文本
,p                                #打印全部编辑文本
this is the first line
this is the second line
this is the third line
this is the fourth line
this is the fifth line
2                                #打印第二行
this is the second line
n                                #显示行号与行内容
2       this is the second line
=                                #显示文本总行数
5
.=                                #显示当前行号
2
i                                #插入文本命令
this is the insert line
.                                #结束插入文本
n                                #显示行号与行内容
2       this is the insert line
,p                                #打印全部编辑文本
this is the first line
this is the insert line
this is the second line
this is the third line
this is the fourth line
this is the fifth line
c                                #更改当前行
this is the first change line
this is the second change line
.                                #结束插入更改文本
,p                                #打印全部编辑文本
this is the first line
this is the first changed line
this is the second changed line
this is the second line
this is the third line
this is the fourth line
this is the fifth line
g/changed/n                        #打印含有“changed”的行号和行内容
2       this is the first change line
3       this is the second change line
2,3d                                #将第2到3行删除
,p                                #打印全部编辑文本
this is the first line
this is the second line
this is the third line
this is the fourth line
this is the fifth line
wq b                                #保存文本内容到 b 文件并退出
117
$ls                                #shell 命令:列出当前文件夹下的文件名
a  b
$

 

 

$ed b                                #ed 编辑 b 文件
117
,p
this is the first line
this is the second line
this is the third line
this is the fourth line
this is the fifth line
2s/second/2th/
,p                                #打印全部编辑文本
this is the first line
this is the 2th line
this is the third line
this is the fourth line
this is the fifth line
,s/this is //                #将所有行中第一个“this is ”替换为“”,即删除
,p                                #打印全部编辑文本
the first line
the 2th line
the third line
the fourth line
the fifth line
wq                                #保存文件并退出
74
$cat b                                #shell 命令,显示文件 b
the first line
the 2th line
the third line
the fourth line
the fifth line
$

 

 

 

$ed b                                #ed 编辑 b 文件
74
,p                                #打印全部编辑文本
the first line
the 2th line
the third line
the fourth line
the fifth line
2,4m5                                #将第2到4行文本内容移到第5行后
,p                                #打印全部编辑文本
the first line
the fifth line
the 2th line
the third line
the fourth line
u                                #撤销上一次命令
,p                                #打印全部编辑文本
the first line
the 2th line
the third line
the fourth line
the fifth line
2t0                                #复制第2行文本到第0行,亦即第一行前
,p                                #打印全部编辑文本
the 2th line
the first line
the 2th line
the third line
the fourth line
the fifth line
u                                #撤销上一次命令
n                                #显示行号与行内容
5       the fifth line
r !date                        #将 shell 命令 date 的输出添加到当前行后
43
,p                                #打印全部编辑文本
the first line
the 2th line
the third line
the fourth line
the fifth line
2008年 09月 03日 星期三  20:49:25 CST
r !cal                                #将 shell 命令 cal 的输出添加到当前行后
145
,p                                #打印全部编辑文本
the first line
the 2th line
the third line
the fourth line
the fifth line
2008年 09月 03日 星期三  20:49:25 CST
       九月  2008
日 一 二 三 四 五 六
    1  2  3  4  5  6
7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
q                                #退出 ed
?                                #“?”提示输入错误,该错误为退出钱没保存文本
wq                                #保存文本并退出
262
$

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值