ed – The Text Editor
ed is the standard UNIX text editor. It is available on all UNIX systems. ed works on a copy of the file. Each ed command is usually a single character, a letter.
Line number of a file in ed is given as 1,2,… The last line is obtained using ‘$’ and current line by ‘.’.
We can start ed editor using ed command followed by file name. If a file exists with the same name the editor will print the number of characters in the file including the new line character. If the file doesn’t exist, it will show a message showing no such file. We can start adding lines using the letter ‘a’. Now we can add the text to the file. To stop adding lines we have to type a ‘.’. To write the lines onto the file enter the letter ‘w’. Now the editor will show the number of characters in the file. To quit the editor we have to type the letter ‘q’. If we try to quit before writing the lines, ed will show a warning ‘?’. If we want to save the lines we can print the letter ‘w’, else quit by typing the letter ‘q’ again.
While in the editor, we can escape to the shell using ‘!’, ie without leaving the ed.
We can print the nth line by typing the command np or just the number n. Lines through m to n can be printed using the command m,np.
/pattern/ finds the next occurrence of pattern. ?pattern? to search backward.
To add a text after line n use the command na.
To insert text before line n use the command ni.
To delete lines m through n use the command m,nd.
To change lines m through n use the command m,nc.
The substitute command is s. s/old/new command replaces first old with new. s/old/new/g command replaces each old with new on the current line. s/old/new/gp command each old into new and print line.
To undo the recent substitution, use the command u.
To move lines m through n to after line d use the command m,nm d.
To copy lines m through n to after line d use the command m,nt d.