There are many Unix/Linux editors
The main line editor is ed
The major screen editors are vi and emacs
All support "regular expressions"
sed is a stream editor which makes heavy use of regular expressions
Utilities such as grep and awk also use regular expressions
The ultimate in line editors
ed file
opens the file - it just
reports the number of bytes read
Commands include
+, -: up, down a line
p: print a line
1,$p: print from first to last line
i, a: insert a line before, after current line
.: exit text entry mode
/pattern/: move to a line matching the pattern
s/pattern1/pattern2/: substitute pattern1 for pattern2
w: write
q: quit
Full-screen editor built on top of ed
It is a
In command mode you can
Use the arrow keys (or +, -, space, backspace) (or h, j, k, l) to move around the file
Delete char (x), word (dw), line (dd)
Use any ed command by prefixing it with ':' e.g. :1,3d deletes lines 1 to 3
Search for text by /pattern/
Change mode to insert by 'a' or 'i'
In insert mode you can
Enter any text
Go back to command mode by ESC
A very flexible, programmable non-modal editor
The X Windows version can be driven from the menus
Keyboard commands are cryptic, usually of the form ^c-^X for some character X e.g. ^c-^w writes the file, ^c-^f opens a file, ^c-_ undoes the last action
Emacs can be extended by adding/modifying commands
using Emacs Lisp, usually stored in ~/.emacs e.g
to jump to a line number
(define-key esc-map "g" 'goto-line)
(define-key menu-bar-search-menu [goto-line]
'("Goto Line" . goto-line))
e.g. reformat the buffer
(defun reformat-buffer ()
;;; "Reformat the buffer, indenting one line at a time"
(interactive "*")
(save-excursion
(goto-char (point-min))
(while (= (forward-line 1) 0)
(c-indent-line))))
(define-key menu-bar-edit-menu [reformat-buffer]
'("Reformat Buffer" . reformat-buffer))
A way to express text patterns for use by editors, etc
Simple patterns
^ matches the start of a line
$ matches the end of a line
. matches any single character
* after a character matches zero or more occurrences of that character
[chars] matches any single one of the characters
Examples:
^The$ matches 'The' on a line by itself.
[Tt]he matches 'The' or 'the'
[0-9]* matches any number
099[0-9]* matches international student ids
sed can use any ed commands to edit a file on the fly
sed '1,10p' file
prints the first 10 lines of the file
sed '1,10d' file
deletes the first 10 lines of the file
sed '/the/d' file
deletes all
lines containing 'the' from the text of the file
sed 's/the/The/' file
substitutes (almost) all
occurrences of 'the' to 'The' in the file
sed 's/ .*//' file
deletes everything after
the first space (substitutes it for nothing)
Stands for "global regular expression print"
Use: grep pattern file
prints all
matching lines in the file
grep '^[0-9][0-9]*$' file
prints all lines containing only numbers
grep ' .* ' file
prints all lines
containing at least two spaces
grep '^[^ ]* [^ ]* [^ ]*$' file
prints all lines containing
awk is a programming language with a pattern matching control structure
All lines are of the form /pattern/ {commands}
awk '/the/ {print $0}' file
prints all lines containing 'the'
awk '{print $1, $3}' file
prints the first and third fields of the file
(separated by spaces)
awk '{print $1, $5}' FS=: /etc/passwd
prints the user id and user name of all users
awk '/^[^ ]* [^ ]* [^ ]*$/ {print $1}' file
prints the first field of all lines with exactly two
spaces
Perl is a programming language designed for text processing
It takes awk and sed to extremes
Print all lines containing 'the'
while (<>) {
if (/the/) {
print $_;
}