grep,sed的祖宗ed

Wayne posted @ Fri, 30 Jul 2010 16:09:07 +0000 in Notes , 4025 readers

 ed,行编辑器,现在看上去很简陋,但是一举一动却透显着各种辉煌后继的渊源.

使用ed,每次可以处理一行.用ed打开文件时,它会定位于最后一行,并显示文件中的字符数.

比如我在电脑上有一份安装时的log:

 

 

[root ~] ll

total 304

...

-rw-r--r-- 1 root root  44611 Feb 22 23:44 install.log

...

 

 

现在用ed打开:

 

 

[root ~] ed install.log

44611

 

 

没有任何提示符,甚至没有告诉你现在所处的行号.虽然说知道是最后一行,但是最后一行是第几行却不知道.直接输入数字,可以跳到相应的第几行,这个采用绝对行号;也可以使用"-","+"来使用相对行号,也就是相对当前行的行号.如"-3",就是往前数三行的内容,不带数字则等于数字为1.  要查看当前行的内容,可以使用"p"命令. man手册上说,"."能显示当前行的address,我实在不明白这个address是什么意思,因为我输入"."后,回显结果跟"p"没有任何区别.

 

 

	[root ~]$ed install.log
44611
.
Installing zsh - 4.2.6-1.i386
p
Installing zsh - 4.2.6-1.i386
 

 

ed既然作为sed跟grep的祖先,自然支持正则表达式,以及正则表达式下的查找替换删除等等操作,甚至连语法也基本一样.所不同的是,它默认只针对当前行进行操作,如果要扩大范围,则需要加地址.比如"g",比如正则表示的地址.它的替换命令"s"的完整格式是:

 

[address]s /pattern/replacement/flag

 

pattern是个正则表达式,replacement则是所要替换成的字符串,flag倘若为"g"的话,则表示会替换这一行内所有出现的pattern,如果没有"g",则默认只替换第一个.

grep实际上就是将"g/re/p"给抽离了出来,单独做成了一个工具."re"表示正则.

sed命令跟ed命令一个很大的不同就是,sed命令如同grep一样,默认带有"g"属性.它加的"[address]"是为了缩小操作范围,而ed的"[address]"是为了扩大操作范围. 当然,从结果上来看,两者可以说没有区别,除非未指定address.

完整的ed命令,都可以在man文档中找到.我个人觉得在vi,sed,awk大行其道的今天,单纯的ed编辑器本身并没有太大必要关注了.但是"行编辑"这个概念,倒是可以给刚开始学习grep,sed之类的人解决不少疑问.

 

SEBA 10th Social Que said:
Tue, 27 Sep 2022 16:51:56 +0000 Every student of Assam Government and Private school class 10th standard student can download SEBA Question Paper for EVS by expert reference to guessing important questions for all chapters of EVS to all mediums of Hindi, English, Assamese, Bengali, Bodo, Manipuri, Garo, Nepali Medium general, SEBA 10th Social Question Paper curriculum and vocational course students to the academic year of 2023.very student of Assam Government and Private school class 10th standard student can download SEBA Question Paper for EVS by expert reference to guessing important questions for all chapters of EVS to all mediums
+1 Blueprint 2024 said:
Sat, 18 Feb 2023 11:44:07 +0000

West Bengal Board Class 11th Blueprint 2024 is Prescribed by West Bengal Council of Higher Secondary Education, In Short WBCHSE. The Board was Established During 1975 and Strives for all-Round Development +1 Blueprint 2024 and Promotion of Higher Secondary Education in the State of West Bengal. The Board Attained Autonomous Status During the Same Year. This Autonomous Body Carries an Extensive Research on Designing Marks Important Marking System.

SCERT Maharashtra 1 said:
Wed, 20 Sep 2023 11:24:39 +0000

Maharashtra Board 1st Class Exam Date Sheet 2024 Available at Official Website. This Maha Board Primary School Exam Attended Every Year More than 50 Laks of Students, MSCE Pune Little Students Studying Maharashtra Class Revised Syllabus 2024 helps Students to Learn Logic and order and hence,Maharashtra Elementary Syllabus 2024 is Designed in Accordance with the NCERT Based SCERT Maharashtra 1st Class Syllabus 2024 Guidelines and helps Students to get an Overview of the Marathi, English Medium All Subject, Students Should Perform well in the Exam, which is Possible by Understanding the Syllabus and Studying Accordingly.To Prepare well for the Maharashtra Class get Promoted to the next Standard, Understanding of the syllabus of Maharashtra State Board for each subject is essential


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter