Sooner or later (sooner) you will have to edit the contents of a file. Unix provides a selection of editors that have evolved over the life of the operating system. Some are very easy to use but limited in their functionality. Others are more powerful but also more challenging to use.
Below is a list of some of the more common editor found on a Unix/Linux system. In general, vi and emacs are considered the standard editors for a experienced Unix user/programmer.
ed - the 1st Unix editor, ed is an interactive editor. However, it is not a screen editor, rather it provides interactive editing at the line level, one line at a time. It also supports application of regular expression type command on a line or range of lines. ed is seldom used anymore at the interactive level, but is still supported on most machines for legacy reasons. diff, a file comparison program, can generate a series ed commands that describe the differences between two files and that can be used by ed to automatically edit the first file to match the second.
patch - is newer utility that can also take the output of diff in order to batch edit a file based on the differences with another file. patch is newer and more flexible.
ex - extended editor is an improved version of ed with greater regular expression support. One of its features is the vi interface which uses the curses library to provide a screen style access to the file being edited.
vi - (vim, vi improved), the visual interface of ex, this allows the user to use the cursor keys to navigate around the document by scrolling up and down the screen will maintaining the regular expression and batch features of ex and ed. vi is the preferred editor for most experienced systems administrators and programmers.
Exercise : To learn vi, log in and run vitutor.
|
nano myfile at the command prompt. At the bottom of the screen will be a listing of available commands as using control characters. You may also read the info pages on nano.
emacs - another advanced screen editor. Written in lisp, emacs provides a very flexible and extensive editing environment. emacs supports a macro language that allows the user to extensively reconfigure it. In fact, our Linux system has the "viper", an emulation overlay for emacs that allows it to use vi commands :). Run :
info viper
to learn more.
It is even possible to use emacs as a command shell. But it also very resource intensive.
On our system, emacs will automatically open an x-terminal on your local desktop, if the services exist. To run emacs in your login terminal screen, use :
emacs -nw
sed - sed is the (s)treaming (ed)itor. This is an editor based on the ed/ex editor series but designed for strictly batch programming. You enter the edits you wish to perform in a "command file". You invoke sed with "command file" and one or more files containing the text you wish to edit and sed applies the edit commands in the "command file" to each line of text read in. We will look at sed in detail later.
awk, nawk, gawk - awk is a report generator. It uses a scripting language modelled after the C language and provides regular expression support. awk takes data input files, a script describing the actions to take on the data and generates formatted output. nawk is an improved version of awk and gawk is gnu's version of awk. Depending on the system, more than one of these may be available. We will cover gawk extensively later.