​A selection of topics on IT and its application to finance. 
​Send me your comments, questions ​or suggestions by clicking
h​ere


elmama
Batch editing a file in UNIX using ed 


Suppose you need to make some batch changes to a file but you don't want to create a temporary file in the process. In UNIX that excludes most of the obvious choices you would normally use such as awk and sed. However there is another way to do this using the ed editor. Most people don't like to use ed since its a line editor and not as easy to use as vi or emacs, but this is one situation where it comes into its own.

As an example suppose you had a file called parse.lex containing the string YYLMAX 200 and you wanted to change it everywhere in the file to the string YYLMAX 500 . Here's how to do it using ed.

First create a script file called edscript containing the ed commands needed to make the changes. The edscript file for the above example would contain the following 4 lines:-

1,$s/YYLMAX 200/YYLMAX 500/
.
w
q


Next simply run the edscript file against the file you wish to change.

$ ed parse.lex < edscript