Editor Command Summary
This section is divided into five major parts, describing the commands in the text editors vi
, ex
, sed
, and awk
, and the pattern-matching syntax common to all of them.
▪ Pattern-Matching Syntax ▪
A number of UNIX text-processing programs, including ed
, ex
, vi
, sed
, and grep
, allow you to perform searches, and in some cases make changes, by searching for text patterns rather than fixed strings. These text patterns (also called regular expressions) are formed by combining normal characters with a number of special characters. The special characters and their use are as follows:
. |
Matches any single character except newline. |
* |
Matches any number (including zero) of the single character (including a character specified by a regular expression) that immediately precedes it. For example, because . means “any character,” .* means “match any number of any characters.” |
[...] |
Matches any one of the characters enclosed between the brackets. For example, [AB] matches either A or B. A range of consecutive characters can be specified by separating the first and last characters in the range with a hyphen. For example, [A–Z] matches any uppercase letter from A to Z, and [0–9] matches any digit from 0 to 9. If a caret (^ ) is the first character in the brackets, the comparison is inverted: the pattern will match any characters except those enclosed in the brackets. |
\ {n,m\ } |
Matches a range of occurrences of the single character (including a character specified by a regular expression) that immediately precedes it. n and m are integers between 0 and 256 that specify how many occurrences to match. \{n\} matches exactly n occurrences, \{n,\} matches at least n occurrences, and \{n,m\} matches any number of occurrences between n and m. For example, A \{2,3 \} matches either AA (as in AARDVARK) or AAA (as in AAA Travel Agency) but will not match the single letter A. This feature is not supported in all versions of vi . |
^ |
Requires that the following regular expression be found at the beginning of the line. |
$ |
Requires that the preceding regular expression be found at the end of the line. |
\ |
Treats the following special character as an ordinary character. For example, \ . stands for a period and \ * for an asterisk. |
\ ( \ ) |
Saves the pattern enclosed between \ ( and \) in a special holding space. Up to nine patterns can be saved in this way on a single line. They can be “replayed” in substitutions by the escape sequences \1 to \9 . This feature is not used in grep and egrep . |
\ n |
Matches the nth pattern previously saved by \ ( and \), where n is a number from 0 to 9 and previously saved patterns are counted from the left on the line. This feature is not used in grep and egrep . |
The egrep
and awk
programs use an extended set of metacharacters:
regexp+ |
Matches one or more occurrences of the regular expression (regexp). |
regexp? |
Matches zero or one occurrences of the regular expression. |
regexp | regexp |
Matches lines containing either regexp. |
(regexp) |
Used for grouping in complex regular expressions (e.g., with | above). |
Regular expressions in ex
(: commands from vi
) offer some different extensions:
\< |
Constrains the following pattern to be matched only at the beginning of a word. |
\> |
Constrains the following pattern to be matched only at the end of a word. |
\u |
Appended to the replacement string of a substitute command, converts first character of replacement string to uppercase. |
\U |
Appended to the replacement string of a substitute command, converts entire replacement string to uppercase. |
\l |
Appended to the replacement string of a substitute command, converts first character of replacement string to lowercase. |
\L |
Appended to the replacement string of a substitute command, converts entire replacement string to uppercase. |
▪ The vi
Editor ▪
Command-Line Syntax
There are two commands to invoke the vi
editor:
vi
[options] [file(s)]
or:
view
[file(s)]
If a file is not named, vi
will open a file that can be given a name with the :f
command or when it is saved using the :w
command. If more than one file is named, the first file is opened for editing and :n
is used to open the next file. The view
command opens the first file for read-only use; changes cannot be saved.
Options:
—l |
Open file for editing LISP programs |
—r |
Recover file |
—R |
Open file in read-only mode; same as using view |
—t tag |
Start at tag |
—x |
Open encrypted file |
+ |
Open file at last line |
+ n |
Open file at line n |
+ / pattern |
Open file at first occurrence of pattern |
−wn |
Set window to n lines |
Operating Modes
After the file is opened, you are in command mode. From command mode, you can invoke insert mode, issue editing commands, move the cursor to a different position in the file, invoke ex
commands or a UNIX shell, and save or exit the current version of the file.
The following commands invoke insert mode:
a A i I o O R s S
While in insert mode, you can enter new text in the file. Press the ESCAPE key to exit insert mode and return to command mode.
Command Syntax
The syntax for editing commands is:
[n] operator [n] object
The commands that position the cursor in the file represent objects that the basic editing operators can take as arguments. Objects represent all characters up to (or back to) the designated object. The cursor movement keys and pattern-matching commands can be used as objects. Some basic editing operators are:
c |
Change |
d |
Delete |
y |
Yank or copy |
If the current line is the object of the operation, then the operator is the same as the object: cc
, dd
, yy
. n is the number of times the operation is performed or the number of objects the operation is performed on. If both n’s are specified, the effect is n times n.
The following text objects are represented:
word |
Includes characters up to a space or punctuation mark. Capitalized object is variant form that recognizes only blank spaces. |
sentence |
Up to . ! ? followed by two spaces. |
paragraph |
Up to next blank line or paragraph macro defined by para = option. |
section |
Up to next section heading defined by sect = option. |
Examples:
2cw |
Change the next two words |
d} |
Delete up to the next paragraph |
d^ |
Delete back to the beginning of the line |
5yy |
Copy the next five lines |
3dl |
Delete three characters to the right of the cursor |
Status Line Commands
Most commands are not echoed on the screen as you input them. However, the status line at the bottom of the screen is used to echo input for the following commands:
/ ? |
Start pattern-matching search forward (/) or backwards (?) |
: |
Invoke an ex command |
! |
Invoke a UNIX command that takes as its input an object in the buffer and replaces it with output from the command |
Commands that are input on the status line must be entered by pressing the RETURN key. In addition, error messages and output from the ^G
command are displayed on the status line.
Summary of vi
Commands
. |
Repeat last command (insert, change, or delete). |
^@ |
Repeat last command. |
@buffer |
Execute command stored in buffer. |
a |
Append text after cursor. |
A |
Append text at end of line. |
^A |
Unused. |
b |
Back up to beginning of word in current line. |
B |
Back up to word, ignoring punctuation. |
^B |
Scroll backward one window. |
c |
Change operator. |
C |
Change to end of current line. |
^C |
Unused. |
d |
Delete operator. |
D |
Delete to end of current line. |
^D |
Scroll down half-window. |
e |
Move to end of word. |
E |
Move to end of word, ignoring punctuation. |
^E |
Show one more line at bottom of window. |
f |
Find next character typed forward on current line. |
F |
Find next character typed back on current line. |
^F |
Scroll forward one window. |
g |
Unused. |
G |
Go to specified line or end of file. |
^G |
Print information about file on status line. |
h |
Left arrow cursor key. |
H |
Move cursor to home position. |
^H |
Left arrow cursor key; BACKSPACE key in insert mode. |
i |
Insert text before cursor. |
I |
Insert text at beginning of line. |
^I |
Unused in command mode; in insert mode, same as TAB key. |
j |
Down arrow cursor key. |
J |
Join two lines. |
^J |
Down arrow cursor key. |
k |
Up arrow cursor key. |
K |
Unused. |
^K |
Unused. |
l |
Right arrow cursor key. |
L |
Move cursor to last position in window. |
^L |
Redraw screen. |
m |
Mark the current cursor position in register (a-z). |
M |
Move cursor to middle position in window. |
^M |
Carriage return. |
n |
Repeat the last search command. |
N |
Repeat the last search command in reverse direction. |
^N |
Down arrow cursor key. |
o |
Open line below current line. |
O |
Open line above current line. |
^O |
Unused. |
p |
Put yanked or deleted text after or below cursor. |
P |
Put yanked or deleted text before or above cursor. |
^P |
Up arrow cursor key. |
q |
Unused. |
Q |
Quit vi and invoke ex |
^Q |
Unused in command mode; in input mode, quote next character, |
r |
Replace character at cursor with the next character you type. |
R |
Replace characters. |
^R |
Redraw the screen, |
s |
Change the character under the cursor to typed characters. |
S |
Change entire line. |
^S |
Unused. |
t |
Move cursor forward to character before next character typed. |
T |
Move cursor back to character after next character typed. |
^T |
Unused in command mode; in insert mode, used with autoindent option set. |
u |
Undo the last change made. |
U |
Restore current line, discarding changes. |
^U |
Scroll the screen upward half-window. |
v |
Unused. |
V |
Unused. |
^V |
Unused in command mode; in insert mode, quote next character. |
w |
Move to beginning of next word. |
W |
Move to beginning of next word, ignoring punctuation. |
^W |
Unused in command mode; in insert mode, back up to beginning of word. |
x |
Delete character under the cursor. |
X |
Delete character before cursor. |
^X |
Unused. |
y |
Yank or copy operator. |
Y |
Make copy of current line. |
^Y |
Show one more line at top of window. |
z |
Redraw the screen, repositioning cursor when followed by CR at the top, . at the middle, and – at the bottom of screen. |
ZZ |
Exit the editor, saving changes. |
^Z |
Unused. |
Characters Not Used in Command Mode
The following characters are unused in command mode and can be mapped as user-defined commands.
^A |
g |
K |
^K |
^O |
q |
^T |
v |
V |
^W |
^X |
^Z |
* |
\ |
_ (underscore) |
|
vi set
Options
The following options can be specified with the :set
command.
Option (Abbreviation) |
Default |
Description |
autoindent (ai) |
noai |
Indents each line to the same level as the line above. Use with shiftwidth option. |
autoprint(ap) |
ap |
Changes are displayed after each editor command, (For global replacement, last replacement displayed.) |
autowrite(aw) |
noaw |
Automatically writes (saves) file if changed before opening another file with :n or before giving UNIX command with : !. |
beautify (bf) |
nobf |
Ignores all control characters during input (except tab, newline, or formfeed). |
directory(dir) |
=tmp |
Names directory in which ex stores buffer files. (Directory must be writable.) |
edcompatible |
noed-compatible |
Uses ed-like features on substitute commands. |
errorbells(eb) |
errorbells |
Error messages ring bell. |
hardtabs(ht) |
-8 |
Defines boundaries for terminal hardware tabs. |
ignorecase(ic) |
noic |
Disregards case during a search. |
lisp |
nolisp |
Indents are inserted in appropriate LISP format. () {} [[ and ]] are modified to have meaning for lisp. |
list |
nolist |
Tabs print as ^I; ends of lines are marked with $. (Used to tell if end character is a tab or a space.) |
magic |
magic |
Wildcard characters . * [ are special in patterns. |
mesg |
mesg |
Permits messages to display on terminal while editing in vi . |
number(nu) |
nonu |
Displays line numbers on left of screen during editing session. |
open |
open |
Allows entry to open or visual mode from ex . |
optimize(opt) |
noopt |
Deletes carriage returns at the end of lines when printing multiple lines; speeds output on dumb terminals when printing lines with leading white space (blanks or tabs). |
paragraphs(para) |
=IPLPPPQP LIpplpipbp |
Defines paragraph delimiters for movement by { or }. The pairs of characters in the value are the names of nroff/troff macros that begin paragraphs. |
prompt |
prompt |
Sets ex prompt (:). |
readonly(ro) |
noro |
Any writes (saves) of a file will fail unless you use ! after the write (works with w , ZZ , or auto write). |
redraw(re) |
noredraw |
Terminal will redraw the screen whenever edits are made (insert mode pushes over existing characters; deleted lines immediately close up). Default depends on line speed and terminal type, no redraw is useful at slow speeds on a dumb terminal; deleted lines show up @, as and inserted text appears to overwrite existing text until you press ESC. |
remap |
remap |
Allows nested map sequences. |
report |
= 5 |
Size of a large edit (i.e., number of lines affected by a single edit) that will trigger a warning message on bottom line of screen. |
scroll |
= [½ window] |
Amount of screen to scroll. |
sections |
=SHNHH HU |
Defines section delimiters for { } movement. The pairs of characters in the value are the names of nroff/troff macros that begin sections. |
shell(sh) |
=/bin/sh |
Pathname of shell used for shell escape (: !) and shell command (: sh ). Value is derived from shell environment. |
shiftwidth (sw) |
sw=8 |
Defines number of spaces to indent when using the ≫ or ≪ commands in the autoindent option. |
showmatch(sm) |
nosm |
In vi , when ) or } is entered, cursor moves briefly to matching ( or {. (If match is not on the screen, rings the error message bell.) Very useful for programming. |
showmode(smd) |
nosmd |
(System V, Release 2 vi only). The string Input Mode is printed on the command line whenever input mode is entered. |
slowopen(slow) |
|
Holds off display during insert. Default depends on line speed and terminal type. |
tabstop(ts) |
= 8 |
Sets number of spaces that a TAB indents during editing session. (Printer still uses system tab of 8.) |
taglength(H) |
= 0 |
Defines the number of characters that are significant for tags. Default (zero) means that all characters are significant. |
tags |
=tags/usr/lib/tags |
Pathname of files containing tags. (See the tag(1) command.) By default, system searches /usr/lib/tags and the file tags in the current directory. |
term |
|
Terminal type. |
terse |
noterse |
Displays briefer error messages. |
timeout |
timeout |
Macros “time out” after 1 second. |
ttytype |
|
Terminal type. |
warn |
warn |
Displays No write since last change as warning. |
window(w) |
|
Shows a certain number of lines of the file on the screen. Default depends on line speed and terminal type. |
wrapscan(ws) |
ws |
Searches wraparound end of file. |
wrapmargin(wm) |
= 0 |
Defines right margin. If greater than zero, automatically inserts carriage returns to break lines. |
writeany(wa) |
nowa |
Allows saving to any file. |
▪ The ex
Editor ▪
The ex
editor is a line editor that serves as the foundation for the screen editor, vi
. All ex
commands work on the current line or a range of lines in a file. In vi
, ex
commands are preceded by a colon and entered by pressing RETURN. In ex
itself, the colon is supplied as the prompt at which you enter commands.
The ex
editor can also be used on its own. To enter ex
from the UNIX prompt:
ex
filename
Any of the options described for invoking vi
may also be used with ex
. In addition, the vi
command Q can be used to quit the vi
editor and enter ex
.
To exit ex
:
x |
Exit, saving changes |
q! |
Quit, without saving changes |
vi |
Enter vi from ex |
To enter an ex
command from vi
:
:address command
options
The colon (:) indicates an ex
command. The address is a line number or range of lines that are the object of the command.
The following options can be used with commands:
! |
Indicates a variant form of the command. |
parameters |
Indicates that additional information can be supplied. A parameter can be the name of a file. |
count |
Is the number of times the command is to be repeated. |
flag |
# , p , and l indicate print format. |
Unlike vi
commands, the count cannot precede the command as it will be taken for the address. d3
deletes three lines beginning with the current line; 3d
deletes line 3.
As you type the address and command, it is echoed on the status line. Enter the command by pressing the RETURN key.
Addresses
If no address is given, the current line is the object of the command. If the address specifies a range of lines, the format is:
x, y
where x and y are the first and last addressed lines. x must precede y in the buffer. x and y may be line numbers or primitives. Using ; instead of , sets the current line to x before interpreting y (that is, the current position willbe at x at the completion of the command). 1,$
addresses all lines in the file.
The following address symbols can be used:
. |
Current line |
n |
Absolute line number |
$ |
Last line |
% |
All lines, same as 1, $ |
x− | +n |
n line before or after x |
−[n] |
One or n lines previous |
+ [n] |
One or n lines ahead |
′x |
Line marked with x |
′ ′ |
Previous context |
/pat/ or ?pat? |
Ahead or back to line matching pat |
ex
Commands
abbrev |
ab [string text] |
|
Define string when typed to be translated into text. If string and text are not specified, list all current abbreviations. |
append |
[address]a [!] |
|
text |
|
. |
|
Append text at specified address, or at present address if none is specified. With the ! flag, toggle the autoindent setting during the input of text. |
args |
ar |
|
Print the members of the argument list, with the current argument printed within brackets ([ ]). |
change |
[address]c [!] |
|
text |
|
. |
|
Replace the specified lines with text. With the ! flag, toggle the autoindent setting during the input of text. |
copy |
[address]c odestination |
|
Copy the lines included in address to the specified destination address. The command t is a synonym for copy . |
delete |
[address]d [buffer] |
|
Delete the lines included in address. If buffer is specified, save or append the text to the named buffer. |
edit |
e [!] [+n]file |
|
Begin editing on file. If the ! flag is used, do not warn if the present file has not been saved since the last change. If the +n argument is used, begin editing on line n. |
file |
f [filename] |
|
Change the name of the current file to filename, which is considered “not edited.” If no filename is specified, print the current status of the file. |
global |
[address]g [!]/pattern/[commands] |
|
Execute commands on all lines that contain pattern. If commands are not specified, print all such lines. If the ! flag is used, execute commands on all lines not containing pattern. |
insert |
[address]i [!] |
|
text |
|
. |
|
Insert text at line before the specified address, or at present address if none is specified. With the ! flag, toggle the autoindent setting during the input of text. |
join |
[address]j [count] |
|
Place the text in the specified range on one line, with white space adjusted to provide two blank characters after a (.), no blank characters if a ) follows, and one blank character otherwise. |
k |
[address]k char |
|
Mark the given address with char. |
list |
[address]l [count] |
|
Print the specified lines in an unambiguous manner. |
map |
map char commands |
|
Define a macro named char in visual mode with the specified sequence of commands. char may be a single character, or the sequence #n, representing a function key on the keyboard. |
mark |
[address]ma char |
|
Mark the specified line with char, a single lowercase letter. Return later to the line with ′x . |
move |
[address]m destination |
|
Move the lines specified by address to the destination address. |
next |
n [!][[+command] filelist] |
|
Edit the next file in the command-line argument list. Use args for a listing of arguments. If filelist is provided, replace the current argument list with filelist and begin editing on the first file; if command is given (containing no spaces), execute command after editing the first such file. |
number |
[address]nu [count] |
|
Print each line specified by address preceded by its buffer line number. # may be used as an abbreviation for number as well as nu . |
open |
[address]o [/pattern/] |
|
Enter open mode at the lines specified by address, or lines matching pattern. Exit open mode with Q . |
preserve |
pre |
|
Save the current editor buffer as though the system had crashed. |
print |
[address]p [count] |
|
Print the lines specified by address with nonprinting characters printed. P may also be used as an abbreviation. |
put |
[address]pu [char] |
|
Restore previously deleted or yanked lines from named buffer specified by char to the line specified by address; if char is not specified, the last deleted or yanked text is restored. |
quit |
q [!] |
|
Terminate current editing session. If the file was not saved since the last change, or if there are files in the argument list that have not yet be accessed, you will not be able to quit without the ! flag. |
read |
[address]r [!] [file] |
|
Copy the text of file at the specified address. If file is not specified, the current filename is used. |
read |
[address]r !command |
|
Read in the output of command into the text after the line specified by address. |
recover |
rec [file] |
|
Recover file from system save area. |
rewind |
rew [!] |
|
Rewind argument list and begin editing the first file in the list. The ! flag rewinds without warning if the file has not been saved since the last change. |
set |
se parameter parameter2 ... |
|
Set a value to an option with each parameter, or if no parameter is supplied, print all options that have been changed from their defaults. For Boolean-valued options, each parameter can be phrased as option or no option; other options can be assigned with the syntax, option=value. |
shell |
sh |
|
Create a new shell. Resume editing when the shell is terminated. |
source |
so file |
|
Read and execute commands from file. |
substitute |
[address]s [[/pattern/repl/]options][count] |
|
Replace each instance of pattern on the specified lines with repl. If pattern and repl are omitted, repeat last substitution. The following options are supported: |
g |
Substitute all instances of pattern |
c |
Prompt for confirmation before each change |
t |
[address]t destination |
|
Copy the lines included in address to the specified destination address. |
ta |
[address]ta tag |
|
Switch the focus of editing to tag. |
unabbreviate |
una word |
|
Remove word from the list of abbreviations. |
undo |
u |
|
Reverse the changes made by the last editing command. |
unmap |
unm char |
|
Remove char from the list of macros. |
v |
[address]v / pattern /[commands] |
|
Execute commands on all lines not containing pattern. If commands are not specified, print all such lines. |
version |
ve |
|
Print the current version number of the editor and the date the editor was last changed. |
visual |
[address]vi [type][count] |
|
Enter visual mode at the line specified by address. Exit with Q. type is either -, ^ or . (see the z command), count specifies an initial window size. |
write |
[address ]w [!] [[≫] file] |
|
Write lines specified by address to file, or full contents of buffer if address is not specified. If file is also omitted, save the contents of the buffer to the current filename. If ≫ file is used, write contents to the end of the specified file. The ! flag forces the editor to write over any current contents of file. |
write |
[address]w !command |
|
Write lines specified by address to command through a pipe. |
wq |
wq [!] |
|
Write and quit the file in one movement. |
xit |
x |
|
Write file if changes have been made to the buffer since last write, then quit. |
yank |
[address]ya [char][count] |
|
Place lines specified by address in named buffer indicated by char. If no char is specified, place in general buffer. |
z |
[address ]z [type][count] |
|
Print a window of text with line specified by address at the top, type is as follows: |
+ |
Place specified line at the top of the window (default) |
− |
Place specified line at bottom of the window |
^ |
Print the window before the window associated with type – |
= |
Place specified line in the center of the window and leave the current line at this line |
|
count specifies the number of lines to be displayed. |
! |
[address]!command |
|
Execute command in a shell. If address is specified, apply the lines contained in address as standard input to command, and replace the lines with the output. |
= |
[address]= |
|
Print the line number of the line indicated by address. |
< > |
[address]<[count] |
|
or [address] > [count] |
|
Shift lines specified by address in specified direction. Only blanks and tabs are shifted in a left shift (<). |
address |
address |
|
Print the lines specified in address. |
RETURN |
RETURN |
|
Print the next line in the file. |
& |
[address]& [options][count] |
|
Repeat the previous substitute command. |
~ |
[address]~ [count] |
|
Replace the previous regular expression with the previous replacement pattern from a substitute command. |
▪ The sed
Editor ▪
sed
[options] file(s)
The following options are recognized:
–n |
Only print lines specified with the p command, or the p flag of the s command |
–e cmd |
Next argument is an editing command |
–f file |
Next argument is a file containing editing command |
All sed
commands have the general form:
[address][, address][!]command [arguments]
The sed
editor copies each line of input into a pattern space. sed
instructions consist of addresses and editing commands. If the address of the command matches the line in the pattern space, then the command is applied to that line. If a command has no address, then it is applied to each input line. It is important to note that a command affects the contents of the space; subsequent command addresses attempt to match the line in the pattern space, not the original input line.
Pattern Addressing
In a sed
command, an address can either be a line number or a pattern, enclosed in slashes (/pattern/). Address types cannot be mixed when specifying two addresses. Patterns can make use of regular expressions, as described at the beginning of this appendix. Additionally, \n
can be used to match any newline in the pattern space (resulting from the N command), but not the newline at the end of the pattern space. If no pattern is specified, command will be applied to all lines. If only one address is specified, the command willbe applied to all lines between the first and second addresses, inclusively. Some commands can only accept one address.
The ! operator following a pattern causes sed
to apply the command to all lines that do not contain the pattern.
A series of commands can be grouped after one pattern by enclosing the command list in curly braces:
[/pattern/][, /pattern/]{
command1
command2
}
Alphabetical List of Commands
: |
:label |
|
Specify a label to be branched to by b or t . label may contain up to eight characters. |
= |
[/pattern/]= |
|
Write to standard output the line number of each line addressed by pattern. |
a |
[address]a \ |
|
text |
|
Append text following each line matched by address. If text goes over more than one line, newlines must be “hidden” by preceding them with a backslash. The insertion will be terminated by the first newline that is not hidden in this way. The results of this command are read into the pattern space (creating a multiline pattern space) and sent to standard output when the list of editing is finished or a command explicitly prints the pattern space. |
b |
[address1] [, address2]b [label] |
|
Branch to label placed with : command. If no label, branch to the end of the script. That is, skip all subsequent editing commands (up to label) for each addressed line. |
c |
[address1] [, address2]c \ |
|
text |
|
Replace pattern space with text. (See a for details on text.) |
d |
[address1] [, address2]d |
|
Delete line in pattern space. Thus, line is not passed to standard output and a new line of input is read; editing resumes with first command in list. |
D |
[address1] [address2]D |
|
Delete first part (up to embedded newline) of multiline pattern created by N command and begin editing. Same as d if N has not been applied to a line. |
g |
[address1][, address2]q |
|
Copy contents of hold space (see h or H command) into pattern space, wiping out previous contents. |
G |
[address1][, address2]G |
|
Append contents of hold space (see h or H command) to contents of the pattern space. |
h |
[address1][, address2]h |
|
Copy pattern space into hold space, a special buffer. Previous contents of hold space are obliterated. |
H |
[address1][, address2]H |
|
Append pattern space to contents of the hold space. Previous and new contents are separated by a newline. |
i |
[address1]i \ |
|
text |
|
Insert text before each line matched by address. (See a for details on text.) |
n |
[address1][, address2]n |
|
Read next line of input into pattern space. Current line is output but control passes to next editing command instead of beginning at the top of the list. |
N |
[address1][, address2]N |
|
Append next input line to contents of pattern space; the two lines are separated by an embedded newline. (This command is designed to allow pattern matches across two lines.) |
p |
[address1][, address2]p |
|
Print the addressed line(s). Unless the –n command-line option is used, this command will cause duplication of the line in the output. Also used when commands change flow control (d , N , b ). |
P |
[address1][, address2]P |
|
Print first part (up to embedded newline) of multiline pattern created by N command. Same as p if N has not been applied to a line. |
q |
[address]q |
|
Quit when address is encountered. The addressed line is first written to output, along with any text appended to it by previous a or r commands. |
r |
[address]r file |
|
Read contents of file and append after the contents of the pattern space. Exactly one space must separate the r and the filename. |
s |
[address1][, address2]s /pattern/replacement/[flags] |
|
Substitute replacement for pattern on each addressed line. If pattern addresses are used, the pattern // represents the last pattern address specified. The following flags can be specified: |
g |
Replace all instances of /pattern/ on each addressed line, not just the first instance. |
p |
Print the line if a successful substitution is done. |
|
If several successful substitutions are done, multiple copies of the line will be printed. |
w file |
Write the line to a file if a replacement was done. A maximum of ten different files can be opened. |
t |
[address1][, address2]t [label] |
|
Test if successful substitutions have been made on addressed lines, and if so, branch to label. (See b and : .) If label is not specified, drop to bottom of list of editing commands. |
w |
[address1][, address2]w file |
|
Write contents of pattern space to file. This action occurs when the command is encountered rather than when the pattern space is output. Exactly one space must separate the w and the filename, A maximum of ten different files can be opened. |
x |
[address1][, address2]x . |
|
Exchange contents of the pattern space with the contents of the hold space. |
▪ awk
▪
An awk
program consists of patterns and procedures:
pattern {procedure]
Both are optional. If pattern is missing, {procedure} will be applied to all lines. If {procedure} is missing, the line will be passed unaffected to standard output (i.e., it will be printed as is).
Each input line, or record, is divided into fields by white space (blanks or tabs) or by some other user-definable record separator. Fields are referred to by the variables $1
, $2
, . . . , $n
. $0
refers to the entire record.
Patterns
Patterns can be specified using regular expressions as described at the beginning of this appendix.
pattern {procedure}
The following additional pattern rules can be used in awk
:
The special pattern BEGIN
allows you to specify procedures that will take place before the first input line is processed. (Generally, you set global variables here.)
Interrupt place after the last input line is processed.
^
and $
can be used to refer to the beginning and end of a field, respectively, rather than the beginning and end of a line.
A pattern can be a relational expression using any of the operators <, <=, ==, !=, >=, and >. For example, $2
> $1
selects lines for which the second field is greater than the first. Comparisons can be either string or numeric.
Patterns can be combined with the Boolean operators ||
(or), &&
(and), and !
(not).
Patterns can include any of the following predefined variables. For example, NF
> 1
selects records with more than one field.
Special Variables
FS |
Field separator (blank and tab by default) |
RS |
Record separator (newline by default) |
OFS |
Output field separator (blank by default) |
ORS |
Output record separator (newline by default) |
NR |
Number of current record |
NF |
Number of fields in current record |
$0 |
Entire input record |
$1, $2, . . . , $n |
First, second,. . . nth field in current record, where fields are separated by FS |
Procedures
Procedures consist of one or more commands, functions, or variable assignments, separated by newlines or semicolons, and contained within curly braces. Commands fall into four groups:
variable or array assignments
printing commands
built-in functions
control flow commands
Variables and Array Assignments
Variables can be assigned a value with an = sign. For example:
FS = “,′′
Expressions using the operators +
, −
, /
, and %
(modulo) can be assigned to variables.
Arrays can be created with the split
function (see following awk
commands) or can be simply named in an assignment statement. ++, +=, and −= are used to increment or decrement an array, as in the C language. Array elements can be subscripted with numbers (array[1],. . . ,array[n]) or with names. (For example, to count the number of occurrences of a pattern, you could use the following program:
/pattern/ {n[″/pattern/″]++ }
END {print n[″/pattern/″] }
awk
Commands
for |
|
|
for (i=lower; i<=upper; i++) command |
|
While the value of variable i is in the range between lower and upper, do command. A series of commands must be put within braces. <= or any relational operator can be used; ++ or −− can be used to decrement variable. |
for |
for i in array command |
|
For each occurrence of variable i in array, do command. A series of commands must be put inside braces. |
if |
if (condition) command |
|
[else] command |
|
If condition is true, do command(s), otherwise do command in else clause, condition can be an expression using any of the relational operators <, <=, ==, ! =, >=, or >, as well as the pattern-matching operator ~ (e.g., if $1 ~ / [Aa] .*/). A series of commands must be put within braces. |
length |
x = length (arg) |
|
Return the length of arg. If arg is not supplied, $0 is assumed. |
log |
x=log (arg) |
|
Return logarithm of arg. |
print |
print [args] |
|
Print args on output, args is usually one or more fields, but may also be one or more of the predefined variables. Literal strings must be surrounded by quotation marks. Fields are printed in the order they are listed. If separated by commas in the argument list, they are separated in the output by the character specified by OFS . If separated by spaces, they are concatenated in the output. |
printf |
printf “format, “expression(s) |
|
Formatted print statement. Fields or variables can be formatted according to instructions in the format argument. The number of arguments must correspond to the number specified in the format sections. |
|
Format follows the conventions of the C language’s printf statement. Here are a few of the most common formats: |
%n.md |
a floating point number; |
|
n = total number of digits. |
|
m = number of digits after decimal point. |
% [–] nc |
n specifies minimum field length for format type c. – justifies value in field; otherwise value is right justified. |
|
Format can also contain embedded escape sequences: \n (newline) or \t (tab) are the most common. |
|
Spaces and literal text can be placed in the format argument by surrounding the entire argument with quotation marks. If there are multiple expressions to be printed, you should specify multiple formats. An example is worth a thousand words. For an input file containing only the line: |
|
5 5 |
The program:
{printf (′′The sum on line %s is %d \n′′, NR, $l+$2) }
will produce:
The sum on line 1 is 10
.
followed by a newline.
split |
x = split (string, array [, sep] ) |
|
Split string into elements of array array [1] , . . . , array [n]. string is split at each occurrence of separator sep. If sep is not specified, FS is used. The number of array elements created is returned. |
sprintf |
x = sprintf (“format”, expression) |
|
Return the value of expression(s), using the specified format (see printf ). |
sqrt |
x = sqrt (arg) |
|
Return square root of arg. |
substr |
x = substr (string, m, [n] ) |
|
Return substring of string beginning at character position m and consisting of the next n characters. If n is omitted, include all characters to the end of string. |
while |
while (condition) command |
|
Do command while condition is true (see if for a description of allowable conditions). A series of commands must be put within braces. |