UNIX_20Shell_20Scripting_20V1.0
UNIX_20Shell_20Scripting_20V1.0
EXAMPLE
#!/bin/sh
Shell Syntax and Constructs – Comments
EXAMPLE
# this text is not
# interpreted by the shell
Shell Syntax and Constructs – Wildcards
EXAMPLE
Filename expansion;
rm *
ls ??
cat file[1-3]
EXAMPLE
echo “What is your name?”
Shell Syntax and Constructs – Local variables
EXAMPLE
variable_name=value
name=“John Doe”
x=5
Shell Syntax and Constructs – Global variables
EXAMPLE
VARIABLE_NAME=value
Export VARIABLE_NAME
PATH=/bin:/usr/bin:.
export PATH
Shell Syntax and Constructs – Extracting values
from variables
To extract the value from variables, a dollar sign is used
EXAMPLE
echo $variable_name
echo $name
echo $PATH
Shell Syntax and Constructs – Reading user input
The read command takes a line of input from the user and assigns it to a variable (s) on
the right-hand side
The read command can accept multiple variables names
Each variable will be assigned a word
EXAMPLE
echo “what is your name?”
read name
read name1 name2 …
Shell Syntax and Constructs – Arguments
(positional parameters)
Arguments can be passed to a script from the command line
Positional parameters are used to receive their values from within the script
EXAMPLE
At the command line:
$ scriptname arg1 arg2 arg3 …
In a script:
echo $1 $2 $3 Positional parameters
echo $* All the positional parameters
echo $# The number of positional parameters
Shell Syntax and Constructs – Arrays (positional parameters)
Bourne shell does not support an array, but a word list can be created by using
positional parameters
A list of words follows the build-in set command, and the words are accessed by
position. Up to nine positions are allowed.
The built-in shift command shifts off the first word on the left-hand side of the list.
The individual words are accessed by position values starting at 1
EXAMPLE
set word1 word2 word3
echo $1 $2 $3 Displays word1, word2, word3
EXAMPLE
variable_name=`command`
echo $variable_name
now=`date`
echo $now
echo “Today is `date`”
Shell Syntax and Constructs - Arithmetic
The Bourne shell does not support arithmetic.
UNIX commands must be used to perform calculations
EXAMPLE
n=`expr 5 + 5`
echo $n
Shell Syntax and Constructs - Operators
The Bourne shell uses the built-in test command operators to test numbers and strings
EXAMPLE
Equality
= String
!= String
-eq Number
-ne Number
Logical
-a AND
-o OR
! NOT
Relational
-gt Greater than
-ge Greater than, equal to
-lt Less than
-le Less than, equal to
Shell Syntax and Constructs–Conditional Statements
The if construct is followed by a command
If an expression is to be tested, it is enclosed in square brackets
The then keyword is placed after the closing parenthesis
An if must end with a fi
EXAMPLE
The if construct is: The if/ else construct is:
if command if [ expression ]
then then
block of statements block of statements
fi else
block of statements
if [ expression ] fi
then
block of statements
fi
Shell Syntax and Constructs–Conditional Statements
EXAMPLE
The if/else/else if construct is: The case command construct is:
if command case variable_name in
then pattern1)
block of statements statements
elfi command ;;
then pattern2)
block of statements statements
else ;;
block of statements *) default value
fi ;;
------------------------------------------------------------------ esac
if [ expression ] ---------------------------------------------------------------------
then case “$color” in
block of statements blue)
elif [ expresssion ] echo $color is blue
then ;;
block of statements green)
else echo $color is green
block of statements ;;
fi *) echo “Not a color” # default
esac
Shell Syntax and Constructs – Loops (while)
Three types of loops: while, until and for
The while loop is followed by a command or an expression enclosed in square brackets,
a do keyword, a block of statements, and terminated with the done keyword. As long as the
expression is true, the body of statements between do and done will be executed.
The loop control commands are break and continue
EXAMPLE
while [ expression]
do
block of statements
done
Shell Syntax and Constructs – Loops (until)
The until loop is just like the while loop, except the body of the loop will be executed as
long as the expression is false
EXAMPLE
until [expression]
do
block of statements
done
Shell Syntax and Constructs – Loops (for)
The for loop used to iterate through a list of words, processing a word and then shifting it
off, to process the next word. When all words have been shifted from the list, it ends.
The for loop is followed by a variable name, the in key word, and a list of words and then
a black of statements, and terminates with the done keyword.
EXAMPLE
EXAMPLE
function_name() {
block of code
}
-------------------------------------------------
lister() {
echo Your present working directory is `pwd`
echo Your files are:
ls
}
grep
grep
grep means “globally search for a regular expression and print out the line
grep command searches for a pattern of characters in a file or multiple files
If the pattern contains whitespace, it must be quoted
The pattern is either a quoted string or a single word (token) and all other words are
treated as filenames
grep sends the output to the screen and does not change or affect the input file in any
way
EXAMPLE
EXPLANATION
• grep will search for the pattern Tom in a file called /etc/passwd.
• If successful, the line from the file will appear on the screen; if the pattern is not found, there will be no output at all; if the
file is not a legitimate, an error will be sent to the screen
• If the pattern is found, grep returns an exit status of 0, indicating success; if the pattern is not found the exist status is 1;
and if the file is not found, the exit status is 2
grep
The grep command can gets its input from a standard input or a pipe, as well as from
files.
If you forgot to name a file, grep will assume it is getting input from standard input, the
keyboard, and will stop until you type something.
If coming from a pipe, the output of a command will be piped as input to the grep
command, and if a desired pattern is matches, grep will print the output to the screen.
EXAMPLE
EXPLANATION
•The output of the ps command (ps –ef displays all processes running on this system) is sent to grep and all lines
containing root are printed
grep - Metacharacters
EXAMPLE
EXPLANATION
The grep command is very useful in shell scripts, because it always returns an exit status
to indicate whether it was able to locate the pattern or the file you were looking for.
If pattern is found, grep returns an exits status of 0, indicating success
If grep cannot find a pattern, it returns 1 as its eit status
And if the file cannot be found, grep returns an exit status of 2
EXAMPLE
EXAMPLE
UNIX Training $>grep NW datafile
northwest NW Charles Main 3.0 .98 3 34
EXPLANATION
Prints all lines containing the regular expression NW in a file called datafile
grep - Examples with Regular expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>grep NW d*
datafile:northwest NW Charles Main 3.0 .98 3 34
db:northwest NW Charles Main 3.0 .98 3 34
EXPLANATION
Prints all lines containing the regular expression NW in all files starting with a d. The shell expands d* to
all files that being with a d. In this case, the filenames retrieved are db and datafile.
grep - Examples with Regular expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>grep '^n' datafile
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
EXPLANATION
Prints all lines beginning with an n. The caret (^) is the beginning-of-line anchor
grep - Examples with Regular expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>grep '4$' datafile
northwest NW Charles Main 3.0 .98 3 34
EXPLANATION
Prints all lines ending with a 4. The dollar sign ($) is the end-of-line anchor.
grep - Examples with Regular expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>grep TB Savage datafile
grep: Savage: No such file or directory
datafile:eastern EA TB Savage 4.4 .84 5 20
UNIX Training $>grep 'TB Savage' datafile
eastern EA TB Savage 4.4 .84 5 20
EXPLANATION
Because the first argument is the pattern and all the remaining arguments are filenames, grep will seach
for TB in a file Savage and a file datafile. To search for TB Savage as complete word, you need to put
them in quotes.
grep - Examples with Regular expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>grep '5\..' datafile
western WE Sharon Gray 5.3 .97 5 23
southern SO Suan Chin 5.1 .95 4 15
northeast NE AM Main Jr. 5.1 .94 3 13
central CT Ann Stephens 5.7 .94 5 13
EXPLANATION
Prints a line containing the number 5, followed by a literal period and any single character. The “dot”
metacharacter represents a single character, unless it is escaped with a backslash. When escaped, the
character is no longer a special metacharacter, but represents itself, a literal period.
grep - Examples with Regular expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>grep '\.5' datafile
north NO Margot Weber 4.5 .89 5 9
EXPLANATION
Prints any line containing the expression .5
grep - Examples with Regular expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>grep '^[we]' datafile
western WE Sharon Gray 5.3 .97 5 23
eastern EA TB Savage 4.4 .84 5 20
EXPLANATION
Prints line beginning with either a w or e. The caret (^) is the beginning-of-line anchor, and either one of
the characters in the brackets will be matched.
grep - Examples with Regular expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>grep '[^0-9]' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
EXPLANATION
Prints all lines containing one nondigit. Because all lines have at least one nondigit, all lines are printed.
grep - Examples with Regular expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>grep 'ss* ' datafile
northwest NW Charles Main 3.0 .98 3 34
southwest SW Lewis Dalsass 2.7 .8 2 18
central CT Ann Stephens 5.7 .94 5 13
UNIX Training $>grep 'sas* ' datafile
southwest SW Lewis Dalsass 2.7 .8 2 18
EXPLANATION
prints all lines containing an s followed by zero or more consecutive occurrences of the letter s and a
space.
grep - Examples with Regular expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>grep '[a-z]\{9\}' datafile
northwest NW Charles Main 3.0 .98 3 34
southwest SW Lewis Dalsass 2.7 .8 2 18
southeast SE Patricia Hemenway 4.0 .7 4 17
northeast NE AM Main Jr. 5.1 .94 3 13
EXPLANATION
Prints all lines where there are at least nine consecutive lowercase letters, for example, northwest,
southwest, southeast and northeast
grep - Examples with Regular expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>grep '\<north' datafile
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
EXPLANATION
Prints all lines containing a word starting with north. The \< is the beginning-of-word anchor
grep - Examples with Regular expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>grep '\<north\>' datafile
north NO Margot Weber 4.5 .89 5 9
EXPLANATION
Prints the line if it contains the word north. The \< is the beginning-of-word anchor, and the \> is the end-
of-word anchor.
grep - Examples with Regular expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>grep '\<[a-z].*n\>' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southern SO Suan Chin 5.1 .95 4 15
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
central CT Ann Stephens 5.7 .94 5 13
EXPLANATION
Prints all lines containing a word starting with a lowercase letter, followed by any number of characters,
and a word ending in n. Watch the .* symbol. It means any characters including whitespace.
grep – Options
Option What it does
-c Displays a count of matching lines rather than displaying the lines that match
-h Does not display filenames
-i Ignores the case of letters in comparisons (i.e., upper and lowercase are
considered identical
-n Preceded each line by its relative line number in the file
-v Inverts the search to display only lines that do not match
-l Lists only the names of the files with matching lines (once), separated by newline
characters
grep - Examples with Options
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>grep -n '^south' datafile
3:southwest SW Lewis Dalsass 2.7 .8 2 18
4:southern SO Suan Chin 5.1 .95 4 15
5:southeast SE Patricia Hemenway 4.0 .7 4 17
EXPLANATION
The –n option precedes each line with the number of the line where the pattern was found, followed by
the line
grep - Examples with Options
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>grep -v 'Suan Chin' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
EXPLANATION
Here, the –v option prints all lines not containing the patter Suan chin.
This option is used when deleting a specific entry from the input file. To really remove the entry, you would redirect the output of grep to a
temporary file, and then change the name of the temporary file back to the name of the original file as shown here:
grep –v ‘Suan Chin’ datafile > temp
mv temp datafile
grep - Examples with Options
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>grep -l 'NW' *
datafile
db
EXPLANATION
The –l option caused grep to print out only the filenames where the pattern is found instead of the line of
text
grep - Examples with Options
northwest NW Charles Main 3 0.98 3 34
EXAMPLE
UNIX Training $>grep -c 'west' datafile
3
EXPLANATION
The –c option causes grep to print the number of lines where the pattern was found. This does not mean
the number of occurrences of the pattern. For example, west was found three times on a line, it only
counts the line once.
grep - Examples with Options
northwest NW Charles Main 3 0.98 3 34
EXAMPLE
UNIX Training $>grep -w 'north' datafile
north NO Margot Weber 4.5 .89 5 9
EXPLANATION
The –w option causes grep to find the pattern only if it is a word, not part of a word. Only the line
containing the word north is printed, not northwest, northeast, etc.
grep - With Pipes
Instead of taking its input from a file, grep often gets its input from a pipe
EXAMPLE
UNIX Training $>ls -l
drwxr-xr-x 2 114199 datastores 4096 2009-04-14 13:37 back
-rw-r--r-- 1 114199 datastores 530 2009-04-14 15:25 datafile
-rw-r--r-- 1 114199 datastores 236 2009-04-14 13:38 db
-rw-r--r-- 1 114199 datastores 0 2009-04-14 16:04 grep
drwxr-xr-x 2 114199 datastores 4096 2009-04-14 16:02 ora_back
The Last Line mode – Writes the content of the buffer to a new file “new_sample.txt””
vi Editor – Switching Between Command and Edit modes
The default mode for the vi editor is the command mode. When you perform an i, o, or a
command, the vi editor switches to the edit mode. After editing a file, press Escape to return
the vi editor to the command mode. Once in the command mode, you can save the file and
quit the vi editor.
The following example shows how to switch modes in the vi editor:
Perform the vi filename command to create a file. You are automatically in the
command mode.
Type the i command to insert text. The i command switches the vi editor to the edit
mode.
Press Escape to return to the command mode.
Perform the :wq command to save the file and exit the vi editor.
vi Editor – vi Command
The vi command enables you to create, edit, and view files in the vi editor.
The syntax for the vi command is:
UNIX Training $> vi
UNIX Training $> vi filename
UNIX Training $> vi options filename
If the system crashes while you are editing a file, you can use the -r option to recover the
file.
To recover a file, perform the command:
UNIX Training $> vi -r filename
The file opens so that you can edit it. You can then save the file and exit the vi editor.
vi Editor – Manipulating Files Within the vi Editor
You can use the vi editor to view the files in the read-only mode, or you can edit in the vi
editor using the vi editing commands.
When using the vi editor, you can move the cursor using certain key sequences.
vi Editor – Viewing files in the Read-only Mode
The view command enables you to view files in the read-only mode. It invokes the vi editor
with the read-only option. Although most of the vi commands are available, you cannot save
changes to the file.
The syntax for the view command is:
UNIX Training $> view filename
The vi editor is case sensitive. Use the appropriate case for the input commands.
vi Editor – Moving the cursor Within the vi Editor
Below table shows the key sequences that move the cursor in the vi editor
Key Sequence Cursor Movement
h, left arrow, or Backspace Left one character
j or down arrow Down one line
k or up arrow Up one line
l, right arrow, orspacebar Right (forward) one character
w Forward one word
b Back one word
e To the end of the current word
$ To the end of the line
0 (zero) To the beginning of the line
^ To the first non-whitespace character on the line
Return Down to the beginning of the next line
vi Editor – Moving the cursor Within the vi Editor
Key Sequence Cursor Movement
G Goes to the last line of the file
1G Goes to the first line of the file
:n Goes to Line n
nG Goes to Line n
Control-F Pages forward one screen
Control-D Scrolls down one-half screen
Control-B Pages back one screen
Control-U Scrolls up one-half screen
Control-L Refreshes the screen
vi Editor – Editing Files Using the vi Editing Commands
Using the Text Deletion Commands
Command Function
Overwrites or replaces characters on the line at and to the right of the
R cursor. To terminate this operation, press Escape.
C Changes or overwrites characters from cursor to the end of the line.
s Substitutes a string for a character at the cursor
x Deletes a character at the cursor
dw Deletes a word or part of the word to the right of the cursor
dd Deletes the line containing the cursor
D Deletes the line from the cursor to the right end of the line
Deletes Lines n through n (For example, :5,10d deletes Lines 5 through
:n,nd 10)
vi Editor – Editing Files Using the vi Editing Commands
Using the Text Changing Commands:
Commands that you can use to change text, undo a change, and repeat an edit
function in the vi editor. Many of these commands change the vi editor to edit mode. To
return to command mode, press Escape.
Command Function
cw Changes or overwrites characters at the cursor location to the end of that word
r Replaces the character at the cursor with one other character
J Joins the current line and the line below
Transposes the character at the cursor and the character to the right of the
xp cursor
~ Changes the case of the letter, either uppercase or lowercase, at the cursor
u Undoes the previous command
U Undoes all changes to the current line
. Repeats the previous command
vi Editor – Editing Files Using the vi Editing Commands
Using the Text Replacing Commands:
Commands that search for and replace text in the vi editor
Command Function
/string Searches forward for the string.
?string Searches backward for the string.
Searches for the next occurrence of the string. Use this command after
n searching for a string.
Searches for the previous occurrence of the string. Use this command
N after searching for a string.
:%s/old/new/g Searches for the old string and replaces it with the new string globally.
vi Editor – Editing Files Using the vi Editing Commands
Using the Text Copying and Pasting Commands
The yy command yanks lines of text and holds a copy of the lines in a temporary
buffer.
The put commands (p, P) inserts the lines of text from the temporary buffer and writes
the text into the current document at the specified location.
The copy (co) and move (m) commands copy or move specified lines to a requested
location within the file.
Command Function
yy Yanks a copy of a line.
p Puts yanked or deleted text under the line containing the cursor.
P Puts yanked or deleted text before the line containing the cursor.
Copies Lines n though n and puts them after Line n. For example, :1,3
:n,n co n co 5 copies Lines 1 through 3 and puts them after Line 5.
Moves Lines n through n to Line n. For example, :4,6 m 8 moves Lines
4 through 6 to Line 8. Line 6 becomes Line 8. Line 5 becomes Line 7.
:n,n m n Line 4 becomes Line 6.
vi Editor – Editing Files Using the vi Editing Commands
Using the File Save and Quit Commands
Commands that save the file and quit the vi editor
Command Function
:w Saves the file with changes by writing to the disk
:w new_filename Writes the contents of the buffer to new_filename
:x Saves the file with changes and quits the vi editor
ZZ Saves the file with changes and quits the vi editor
:q! Quits without saving changes
:wq Saves the file with changes and quits the vi editor
vi Editor – Customizing a vi session
You can customize a vi session by setting variables for the session. When you set a vi
variable, you enable a feature that is not activated by default. You can use the set
command to enable and disable variables.
Below are some of the variables of the set command, including displaying line
numbers and invisible characters, such as the Tab and the end-of-line characters.
Command Function
:set nu Shows line numbers
:set nonu Hides line numbers
:set ic Instructs searches to ignore case
:set noic Instructs searches to be case sensitive
Displays invisible characters, such as ^I for a Tab and a $ for
:set list end-of-line characters
:set nolist Turns off the display of invisible characters
:set showmode Displays the current mode of operation
:set noshowmode Turns off the mode of operation display
:set Displays all the vi variables that are set
:set all Displays all set vi variables and their current values
vi Editor – Customizing a vi session
To create an automatic customization for all of your vi sessions, complete the following
steps:
Create a file in your home directory named .exrc.
Enter the set variables into the .exrc file.
Enter set variable without the preceding colon.
Perform one command on one line.
The vi editor reads the .exrc file located in your home directory each time you open a vi
session, regardless of your current working directory.
vi Editor – Review
sed
sed – the Streamlined Editor
sed is a streamlined, non interactive editor.
Allows you to perform the same kind of editing tasks used in vi editor.
Instead of working interactively with the editor, the sed program lets you type your editing
commands at the command line, name of the file, and then see the output of the editing
command on the screen
The sed editor is non destructive. It does not change your file unless you save the output with
shell redirection.
All the lines are printed to the screen by default.
You can also put sed commands in a file called a sed script, if you need to perform multiple
edits.
sed – How does sed Work?
1. The sed editor processes a file (or input) one line at a time and sends its output to the
screen.
2. Its commands are similar to those used in vi editor
3. sed stores the line it is currently processing in a temporary buffer called a pattern space or
temporary buffer.
4. Once sed is finished processing the line in the pattern space (i.e., executing sed commands
on that line), the line in the pattern space is sent to the screen (unless the command was to
delete the lines or suppress its printing).
5. After the line has been processed, it is removed from the pattern space and the next line is
then read into pattern space, processed, and displayed.
6. sed ends when the last line of the input file has been processed.
7. By storing each line in a temporary buffer and performing edits on that line, the original file is
never altered or destroyed.
FORMAT
sed ‘command’ filename (s)
EXAMPLE
UNIX Training $>sed '1,6d' datafile
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
UNIX Training $>sed -n '/[Hh]emenway/p' datafile
southeast SE Patricia Hemenway 4.0 .7 4 17
EXPLANATION
Lines 1 through 6 of datafile are deleted
Only lines matching the pattern Hemenway or hemenway in datafile are printed
sed – Commands
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
FORMAT
sed ‘command’ filename (s)
EXAMPLE
UNIX Training $>sed '1,5d' datafile
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
UNIX Training $>sed -n '/[Hh]emenway/p' datafile
southeast SE Patricia Hemenway 4.0 .7 4 17
EXPLANATION
Lines 1 through 5 of datafile are deleted
Only lines matching the pattern Hemenway or hemenway in datafile are printed
sed – Commands
Command Function
a\ Appends one or more lines of text to the current line
c\ Changes (replaces) text in the current line with new text
d Deleted lines
i\ Inserts text above the current line
h Copies the contents of the pattern space to a holding buffer
H Appends the contents of the pattern space to a holding buffer
g Gets what is in the holding buffer and copies it into the pattern buffer,
overwriting what was there
G Gets what is in the holding buffer and copies it into the pattern buffer,
appending what was there
l Lists nonprinting characters
p Prints lines
sed – Commands
Command Function
n Reads the next input line and starts processing the newline with the next
command rather than the first command
q Quits or exists sed
r Reads lines from a file
! Applies the command to all lines except the selected ones
s Substitutes one string for another
Substitution Flags
g Globally substitutes on a line
p Prints lines
w Writes lines out to a file
x Exchanges contents of the holding buffer with the pattern space
y Translates one character to another (cannot use regular expression
metacharacters with y)
sed – Options
Command Function
-e Allows multiple edits
EXAMPLE
UNIX Training $>sed '/Suan/d' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
UNIX Training $>sed '/Suan/!d' datafile
southern SO Suan Chin 5.1 .95 4 15
EXPLANATION
First command tells sed to delete all lines containing the pattern Suan
Second command tells sed to delete all lines not containing the pattern Suan
sed – Options
EXAMPLE
UNIX Training $>sed '/Suan/d' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
UNIX Training $>sed '/Suan/!d' datafile
southern SO Suan Chin 5.1 .95 4 15
EXPLANATION
First command tells sed to delete all lines containing the pattern Suan
Second command tells sed to delete all lines not containing the pattern Suan
sed – How to modify a file with sed
sed is a non destructive editor.
It will display the edits you make on your screen, but it will not change the file you are
editing. To really reflect the edits in the file, you must redirect the output to another file, an
then rename the original file.
EXAMPLE
UNIX Training $>sed '1,6d' datafile > temp
UNIX Training $>cat temp
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
UNIX Training $>mv temp datafile
UNIX Training $>cat datafile
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
EXPLANATION
Lines 1 through 6 of datafile are deleted. Instead of displaying the remaining lines on the screen, they are
redirected to a file called temp.
The mv command will overwrite datafile with the contents of temp
sed – Error messages and exit status
If the syntax is error-free, the exit status that sed returns to the shell is zero and a nonzero
integer for failure
EXAMPLE
UNIX Training $>sed '1,3d' datafile
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
UNIX Training $>echo $?
0
UNIX Training $>sed '1,3d' datafile2
sed: can't read datafile2: No such file or directory
UNIX Training $>echo $?
2
UNIX Training $>sed 's/Lewis/g' datafile
sed: -e expression #1, char 9: unterminated `s' command
UNIX Training $>echo $?
1
EXPLANATION
The first command is a success (exit status = 0)
The second one is failure as the file was not available (exit status = 1)
The third one is a syntax error (exit status = 2)
sed – Regular Expression Metacharacters
Metacharacter Function Example What it matches
^ Beginning-of-line anchor ‘^east’ Matches all lines beginning with
east
$ End-of-line anchor ‘east$’ Matches all lines ending with east
. Matches one character ‘e..t’ Matches lines containing an e,
followed by two characters,
followed by an e
* Matches zero or more ‘ *east’ Matches lines with zero or more
characters preceding the spaces, followed by the pattern
asterisk east
[] Matches one character in ‘[Ee]ast Matches lines containing east or
the set East
[^] Matches one character ‘[^A- Matches lines not containing a
not in the set K]ast’ character in the range A through
K, followed by ast
\< Beginning-of-the word ‘\<east’ Matches lines containing a word
anchor that begins with east
\> End-of-word anchor ‘east\>’ Matches lines containing a word
that ends with east
sed – Examples (Printing: The p Command)
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>sed '/north/p' datafile
northwest NW Charles Main 3.0 .98 3 34
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
EXPLANATION
Prints all lines to standard output by default. If the pattern, north is found, sed will print that line in addition to all the other
lines
sed – Examples (Printing: The p Command)
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
EXAMPLE
UNIX Training $>sed -n '/north/p' datafile
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
EXPLANATION
By default, sed prints the line currently in pattern buffer. With the p command, sed is told to print the line again. The –n
option suppresses the default behavior of sed. When used with the p command, the line in the pattern buffer is printed only
once. Without the –n option, sed will print duplicate lines of output as shown in the previous example.
Only the lines containing the pattern north are printed when –n is used.
sed – Examples (Deleting: The d command)
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
EXAMPLE
UNIX Training $>sed '3d' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
EXPLANATION
Deletes the third line. All other lines are printed to the screen by default.
sed – Examples (Deleting: The d command)
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
EXAMPLE
UNIX Training $>sed '3,$d' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
EXPLANATION
The third line through the last line are deleted. The dollar sign ($) represents the last line of the file. The comma is called
the range operator. The remaining lines are printed.
In this example, the range of addresses starts at line 3 and ends at the last line, which is represneted by the dollar sign ($)
sed – Examples (Deleting: The d command)
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
EXAMPLE
UNIX Training $>sed '$d' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
EXPLANATION
Deletes the last line. The dollar sign ($) represents the last line. The default is to print all of the lines except those effected
by the d command.
sed – Examples (Substitution: The s command)
northwest NW Charles Main 3 0.98 3 34
EXAMPLE
UNIX Training $>sed 's/west/north/g' datafile
northnorth NW Charles Main 3.0 .98 3 34
northern WE Sharon Gray 5.3 .97 5 23
southnorth SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
EXPLANATION
The s command is for substitution. The g flag at the end of the command indicates that the substitution is global across
the line; that is, if multiple occurrences of west are found, all of them will be replaced with north. Without the g command,
only the first occurrence of west on each line would be replaced with north
sed – Examples (Substitution: The s command)
northwest NW Charles Main 3 0.98 3 34
EXAMPLE
UNIX Training $>sed -n 's/^west/north/p' datafile
northern WE Sharon Gray 5.3 .97 5 23
EXPLANATION
The s command is for substitution. The –n option with the p flag at the end of the command tells sed to print only those
lines where the substitution occurred; that is, if west is found at the beginning of the line and is replaces with north, just
those lines are printed.
sed – Examples (Substitution: The s command)
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
EXAMPLE
UNIX Training $>sed -n 's/Hemenway/Jones/gp' datafile
southeast SE Patricia Jones 4.0 .7 4 17
EXPLANATION
All occurrences of Hemenway are replaces with Jones, and only the lines that are changes are printed. The –n option
combined with the p command suppresses the default output. The g stands for global substitution across the line.
sed – Examples (Substitution: The s command)
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
EXAMPLE
UNIX Training $>sed -n 's/\(Mar\)got/\1ianne/p' datafile
north NO Marianne Weber 4.5 .89 5 9
EXPLANATION
The pattern Mar is enclosed in parentheses and saved as tag 1 in a special register. It will be referenced in the
replacement in the replacement string as \1. Margot is then replaced with Marianne.
sed– Examples (Range of Selected Lines: The Comma)
A range of lines starts at one address in a file and ends at another address in the file.
The address range can be line numbers (such as 5,10), regular expressions (such as
/Dick/,/Joe/), or a combination of both (such as /north/,$).
The range is inclusive – it consists of the line with the starting condition, all lines in between,
and the line ending the condition of the range.
If the ending condition is never reached, it ends when the file ends.
If the ending condition is reached, and then the condition for starting sgain applies, the range
starts over.
sed– Examples (Range of Selected Lines: The Comma)
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>sed -n '/west/,/east/p' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
EXPLANATION
All lines in the range of patterns between west and east are printed. If west were to appear on a line after east, the lines
from west to the next east or to the end of the file, whichever comes first, would be printed. The arrows mark the range.
sed– Examples (Range of Selected Lines: The Comma)
northwest NW Charles Main 3 0.98 3 34
EXAMPLE
UNIX Training $>sed -n '/north/,/west/p' datafile
northwest NW Charles Main 3.0 .98 3 34
R1
western WE Sharon Gray 5.3 .97 5 23
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
R2 (end of file reached even before range ends)
central CT Ann Stephens 5.7 .94 5 13
EXPLANATION
All lines in the range of patterns between north and west are printed. The arrows mark the range.
sed– Examples (Range of Selected Lines: The Comma)
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>sed -n '5,/northeast/p' datafile
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
EXPLANATION
Prints the lines from line 5 through the first line that begins with northeast
sed– Examples (Range of Selected Lines: The Comma)
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>sed '/west/,/east/s/$/**VACA**/' datafile
northwest NW Charles Main 3.0 .98 3 34**VACA**
western WE Sharon Gray 5.3 .97 5 23**VACA**
southwest SW Lewis Dalsass 2.7 .8 2 18**VACA**
southern SO Suan Chin 5.1 .95 4 15**VACA**
southeast SE Patricia Hemenway 4.0 .7 4 17**VACA**
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
EXPLANATION
For lines in the range between the patterns east and west, the end-of-line ($) is replaes with the string **VACA**. The
newline is moved over to the end of the new string.
sed – Examples (Multiple Edits: The e Command)
The –e command is the edit command, and is used if you have more than one editing
command for sed to execute. All edits are applied to the line in the pattern buffer, before the
next line is edited
sed – Examples (Multiple Edits: The e Command)
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>sed -e '1,3d' -e 's/Hemenway/Jones/' datafile
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Jones 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
EXPLANATION
The –e option allows multiple edits. The first edit removes lines 1 through 3. the second edit substitutes Hemenway with
Jones. Because both edits are done on a per-line basis (i.e., both commands are executed on the current line in the
pattern space), the order of the edits may affect the outcome. For example, if both commands had performed substitutions
on the line, the first substitution could affect the second substitution.
sed–Examples (Reading from files: The r Command)
The r command is the read command. It allows sed to load content from one text file into the
current file at some specified location.
+++++++++++++++++++++++++++++++++++++++++++++++
newfile *** SUAN HAS LEFT THE COMPANY ***
+++++++++++++++++++++++++++++++++++++++++++++++
sed–Examples (Reading from files: The r Command)
EXAMPLE
UNIX Training $>sed '/Suan/r newfile' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
+++++++++++++++++++++++++++++++++++++++++++++++
*** SUAN HAS LEFT THE COMPANY ***
+++++++++++++++++++++++++++++++++++++++++++++++
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
EXPLANATION
The r command reads specified lines from a file. The contents of the newfile are read into the input file datafile, after after
the line where the pattern Suan is matches. If Suan has appeared on more than one line, the contents of newfile would
have been read in under each occurrence.
sed–Examples (Writing to Files: The w Command)
The w command is the write command. It allows sed to write lines from the current file into
another file.
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>sed -n '/north/w newfile' datafile
UNIX Training $>cat newfile
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
EXPLANATION
The w command writes specified lines to a file. All lines containing the pattern north are written to a file called newfile.
sed–Examples (Appending: The a Command)
The a command is the append command. Appending causes new text to be places below the
current line in the file; that is, the line that was just read into the pattern buffer. Whether at the
command line or in sed script, the a command is followed by a backslash.
EXAMPLE
UNIX Training $>sed '/eastern/{n;s/AM/Archie/;}' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE Archie Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
EXPLANATION
If the pattern eastern is matches on a line, the n command causes sed to get the next line of input (the line with AM Main
jr.) replace the pattern space with this new line, substitute AM with Archie, print the line, and continue.
sed–Examples (Transform: The y Command)
The y command is the transform command. Characters are translated on a one-to-one,
corresponding bases from those on the left hand side to those on the right hand side.
For example, in the command y/abc/ABC/ any lowercase letter a will be transformed into an
uppercase A, as will b to B, and c to C
sed–Examples (Transform: The y Command)
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>sed '1,3y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' datafile
NORTHWEST NW CHARLES MAIN 3.0 .98 3 34
WESTERN WE SHARON GRAY 5.3 .97 5 23
SOUTHWEST SW LEWIS DALSASS 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
EXPLANATION
The lines 1 through 3, the y command translates all lowercase letters to uppercase letters.
sed–Examples (Quit: The q Command)
The q command is the quit command. It causes the sed program to exit without further
processing.
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>sed '5q' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
EXPLANATION
After the line 5 is printed, the q command causes the sed program to quit
sed–Examples (Quit: The q Command)
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>sed '/Lewis/{s/Lewis/Joseph/;q}' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Joseph Dalsass 2.7 .8 2 18
EXPLANATION
When the pattern Lewis is matches on a line, first the substitution command (s) replaces Lewis with Joseph, and then the q
command causes the sed program to quit.
sed–Examples (Holding and Getting: The h and g Commands)
The h command is for holding; the g command is for getting.
When a line is read in from a file, sed places it in the pattern buffer and excutes commands
on that line. There is an additional buffer called the holding buffer. Both buffers can hold upto
8,192 bytes.
A line can be sent from the pattern buffer to a holding buffer with the h command, later can be
retrieved with the g or G (get) command.
sed–Examples (Holding and Getting: The h and g Commands)
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>sed -e '/northeast/h' -e '$G' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13 holds
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
northeast NE AM Main Jr. 5.1 .94 3 13 appends
EXPLANATION
After the line containing the pattern northeast is found, it is placed in the pattern space and the h command copies it and
places it into another special buffer called the holding buffer. In the second sed instruction, when the last line is reached ($)
and the G command tells sed to get the line from the holding buffer and put it back in the pattern space buffer, appending it
to the line that is currently stored there, in this case, the last line.
Simply stated: Any line containing the pattern northeast will be copied and appended to the end of the line
sed–Examples (Holding and Getting: The h and g Commands)
northwest NW Charles Main 3 0.98 3 34
EXAMPLE
UNIX Training $>sed -e '/WE/{h;d;}' -e '/CT/{G;}' datafile
northwest NW Charles Main 3.0 .98 3 34
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
western WE Sharon Gray 5.3 .97 5 23
EXAMPLE
UNIX Training $>sed -e '/WE/{h;d;}' -e '/CT/{g;}' datafile
northwest NW Charles Main 3.0 .98 3 34
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
western WE Sharon Gray 5.3 .97 5 23
The line containing WE is replacing the line containing CT (g option). The d in the first edit is to delete the line with WE
from displaying
sed–Examples (Holding and Exchanging: The h and x Commands)
The x command is the exchange command. It lets sed exchange the pattern in the holding
buffer with what is currently in the pattern buffer.
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
EXAMPLE
UNIX Training $>sed -e '/Patricia/h' -e '/Margot/x' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
southeast SE Patricia Hemenway 4.0 .7 4 17
central CT Ann Stephens 5.7 .94 5 13
The line containing Margot will be replaced with the line containing Patricia
sed - Scripting
A sed script is a list of sed commands in a file.
To let sed know your commands are in a file, when invoking sed at the command line, use
the –f option followed by the name of the sed script.
sed is very particular about the way you type lines into the script. There cannot be any
trailing whitespace or text at the end of the command.
A line from the input file is copied into the pattern buffer, and all commands in the sed script
are executed on that line. After the line has been processed, the next line from the input file is
placed in the pattern buffer, and all commands in the script are executed on that line.
Quotes are not needed to protect sed commands from interpretation by the shell and only
one backslash is used in line continuation.
You cannot use quotes in a sed script at all, unless they are part of a search pattern.
sed – Scripting Example
UNIX Training $>cat sedscript1
1 # sed script This line is a comment. Comments must be on lines by
themselves and start with a pound sign(#)
2 /Lewis/a\ if a line contains the pattern Lewis, the next three lines are
appended to that line.
3 Lewis is the TOP Salesperson for April!!\ Each line being appended, except the last one, is terminated
with a backslash. The backslash must be immediately
Lewis is moving to the southern district next month.\
followed with a newline. If there is any trailing text, even one
space after the newline, sed will complain.
4 CONGRATULATIONS! The last line to be appended does not have a backslash. This
indicates to sed that this is the last line to be appended and
that the next line is another command.
5 /Margot/c\ Any lines containing the pattern Margot will be replaced (c
command) with the next three lines of text
***************************************************************\
MAGOT HAS RETIRED\
***************************************************************
6 1i\ The next two lines will be inserted (I command) above line 1.
EMPLOYEE DATABASE\
-----------------------------
7 $d The last line ($) will be deleted.
sed – Scripting Example
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
northeast NE AM Main Jr. 5.1 0.94 3 13
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EMPLOYEE DATABASE
-----------------------------
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
Lewis is the TOP Salesperson for April!!
Lewis is moving to the southern district next month.
CONGRATULATIONS!
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
***************************************************************
MAGOT HAS RETIRED
***************************************************************
sed – Review
Command What it does
sed –n ‘/sentimental/p’ filex Prints to the screen all lines containing sentimental. The file filex
does not change. Without the –n option, all lines with sentimental
will be printed twice
sed ‘1,3d’ filex > newfilex Deletes lines 1,2, and 3 from filex and saves changes to newfilex
sed ‘[Dd]aniel/d’ filex Deletes lines containing Daniel and daniel
sed –n ’15,20p’ filex Prints only lines 15 through 20
sed ‘1,10s/Montanana/MT/g’ filex Substitutes Montana with MT globally in lines 1 through 10
sed ‘/March/!d’ filex Deletes all lines not containing March
sed ‘/report/s/5/8/’ filex Changes the first occurrence of 5 to 8 on all lines containing report
sed ‘s/….//’ filex Deletes the first four characters of each line
sed ‘s/…$//’ filex Deletes the last three characters of each line
sed ‘/east/,/west/s/North/South/’ filex For any lines falling in the range from east to west, substitutes
North with South
sed –n ‘/Time off/w timefile’ filex Writes all lines containing Time off to the file timefile
sed ‘s/\([Oo]ccur\)ence/\1rence/’ filex Substitutes either Occurrence or occurrence with Occurrence or
occurrence
awk
awk
awk is a UNIX programming language used for manipulating data and generating reports
The data may come from standard input, one or more files, or as output from a porcess.
awk can be used at the command line for simple operations, or it can be written into
programs for larger applications
Because awk can manipulate data, it is an indispensable toll used in shell scripts and for
managing small databases.
awk scans a file (or input) line by line, from the first to the last line, searching for lines that
match a specified pattern and performing selected actions (enclosed in curly braces) on those
lines.
If there is a pattern with no specific action, all lines that match the pattern are displayed; if
there is an action with no pattern, all input lines specified by the action are executed upon.
awk – Input from files
FORMAT
awk ‘pattern’ filename
awk ‘{action}’ filename
awk ‘pattern {action}’ filename
EXAMPLE
UNIX Training $>awk '/Mary/' employees
Mary Adams 5346 11/4/63 28765
EXPLANATION
awk prints all lines that contain the pattern Mary
awk – Input from files
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500
EXAMPLE
UNIX Training $>awk '{print $1}' employees
Tom
Mary
Sally
Billy
EXPLANATION
awk prints the first filed of the file employees, where the files starts at the left margin of the line and is
delimited by whitespace.
EXAMPLE
UNIX Training $>awk '/Sally/{print $1,$2}' employees
Sally Chang
EXPLANATION
awk prints the first and second fields of the file employees, only if the line contains the pattern Sally.
Remember, the filed seperator is whitespace.
awk – Input from Commands
The output from a UNIX command or commands can be piped to awk for processing.
Shell programs commonly use awk for manipulating commands.
FORMAT
command | awk ‘pattern’ filename
command | awk ‘{action}’ filename
command | awk ‘pattern {action}’ filename
EXAMPLE
UNIX Training $>df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda5 12847900 6406532 5788732 53% /
/dev/sda3 4134384 1542556 2381808 40% /home
/dev/sda1 194442 12573 171830 7% /boot
tmpfs 256736 0 256736 0% /dev/shm
UNIX Training $>df | awk '$4 > 750000'
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda5 12847900 6406532 5788732 53% /
/dev/sda3 4134384 1542556 2381808 40% /home
EXPLANATION
The df command reports the free disk space on file systems. The output of the df command is piped to
awk. If the fourth field is greater than 750000 blocks, the line is sprinted.
awk – Input from Commands
EXAMPLE
UNIX Training $>ls -l
-rw-r--r-- 1 114199 datastores 530 2009-04-14 17:36 abc
drwxr-xr-x 2 114199 datastores 4096 2009-04-14 13:37 back
-rw-r--r-- 1 114199 datastores 530 2009-04-14 15:25 datafile
-rw-r--r-- 1 114199 datastores 117 2009-04-15 14:54 datafile1
-rw-r--r-- 1 114199 datastores 236 2009-04-14 13:38 db
-rw-r--r-- 1 114199 datastores 124 2009-04-16 13:25 employees
-rw-r--r-- 1 114199 datastores 124 2009-04-15 20:56 employees2
-rw-r--r-- 1 114199 datastores 134 2009-04-15 16:55 newfile
-rw-r--r-- 1 114199 datastores 176 2009-04-15 17:06 newfile1
-rw-r--r-- 1 114199 datastores 642 2009-04-14 18:06 new_sample.txt
drwxr-xr-x 2 114199 datastores 4096 2009-04-14 16:02 ora_back
-rw-r--r-- 1 114199 datastores 642 2009-04-14 18:37 sample_text.txt
-rw-r--r-- 1 114199 datastores 0 2009-04-15 17:11 sed
-rw-r--r-- 1 114199 datastores 356 2009-04-15 20:16 sedscript1
UNIX Training $>ls -l | awk '/2009-04-16/{print $8}'
employees
EXPLANATION
ls –l gives the listing of the files. The second command uses awk to serach for the files created on 16th April 2009 and
prints them.
awk – Formatting Output (The print function)
The print function is used to print simple output that does not require fancy formatting.
The print function accepts arguments as variables, computed values, or string constants.
Strings must be enclosed in double quotes.
Commas are used to separate arguments; if commas are not provided, the arguments are
concatenated together.
The comma evaluates to the value of OFS, which is by default a space.
The output of the print function can be redirected or piped to another program, and output of
another program can be pipled to awk for printing.
EXAMPLE
UNIX Training $>date
Thu Apr 16 13:33:17 IST 2009
UNIX Training $>date | awk '{print "Month: " $2"\nYear: ", $6 }'
Month: Apr
Year: 2009
EXPLANATION
The output of the UNIX date command will be piped to awk. The string Month: is printed, followed by the
second field, the string containing the newline character (\n), and Year:, followed by the sixth field ($6).
awk – Formatting Output (The print function)
Escape sequences are represented by a backslash and a letter or number. They can used in
strings to represent tabs, newlines, form feeds, and so forth.
EXAMPLE
UNIX Training $>awk '/Sally/{print "\t\tHave a nice day, " $1, $2 "!"}' employees
Have a nice day, Sally Chang!
EXPLANATION
If the line contains the pattern Sally, the print function prints two tabs, the string Have a nice day, the first
(where $1 is Sally) and second fields (where $2 is Chang), followed by a string containing an
exclamation mark.
awk – Formatting Output (The print function)
OFMT Variable:
when printing numbers you may want to control the format of the number.
OFMT variable can be set to control the printing of numbers when using the print function.
It is set by default to %.6g – six significant digits to the right of the decimal are printed.
EXAMPLE
UNIX Training $>awk 'BEGIN{OFMT="%.10f";print 1.245678909,12E-2}'
1.2456789090 0.1200000000
EXPLANATION
The OFMT variable is set so that floating-point numbers (f) will be printed with two numbers following the
decimal point. The percent (%) sign indicates a format being specified. Here the variable is changed to
set to 10 decimal digits.
awk – Formatting Output (The printf function)
The printf function returns a formatted string to standard output.
The printf statement consists of a quoted control string that may be embedded with format
specifications and modifiers.
EXAMPLE
UNIX Training $>echo "UNIX"|awk '{printf "|%-15s|\n",$1}'
|UNIX |
UNIX Training $>echo "UNIX"|awk '{printf "|%15s|\n",$1}'
| UNIX|
EXPLANATION
The output of the echo command, UNIX, is piped to awk. The printf function contains a control string.
The percent sign alerts printf that it will be printing a 15 space, left-justified enclosed in vertical bars and
terminated with a newline. The dash after the percent sign (%) indicates left-justification. The control
string is followed by a comma and $1, The string UNIX will be formatted according to the format string in
the control string.
The string UNIX is printed in a right-justified, 15 space string, enclosed in vertical bars, and terminated
with a new line.
awk – Formatting Output (The printf function)
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500
EXAMPLE
UNIX Training $>awk '{printf "%-10s %-10s %10s %10s %10s\n",$1,$2,$3,$4,$5}' employees
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500
UNIX Training $>awk '{printf "The name is: %-15s ID is %8d\n", $1, $3}' employees
The name is: Tom ID is 4424
The name is: Mary ID is 5346
The name is: Sally ID is 1654
The name is: Billy ID is 1683
EXPLANATION
The lines from the file are printed as per formatting specified
The lines from the file are printed as per formatting specified
awk – awk Commands from Within a File
If the awk commands are placed in a file, the –f option us used with the name of the awk file,
followed by the same of the input file to be processed.
A records is read into awk’s buffer and each of the commands in the awk file is tested and
executed for that record.
After the awk has finished with the first record, it is discarded and the next record is read into
the buffer, and so on.
If the action is not controlled by a pattern, the default behavior is to print the entire record.
If the pattern does not have an action associated with it, the default is to print the record
where the pattern matches an input line.
awk – awk Commands from Within a File
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500
EXAMPLE
UNIX Training $>cat awkfile
/^Mary/{print "Hello Mary!"} If the record begins with the regular expression Mary, the string Hello
Mary! is printed. The action is controlled by the pattern preceding it.
Fields are separated by whitespace.
{print $1, $2, $3} The first, second, and third field of each record are printed. The
action occurs for each line because theer is not a pettern controlling
the action.
UNIX Training $>awk -f awkfile employees
Tom Jones 4424
Hello Mary!
Mary Adams 5346
Sally Chang 1654
Billy Black 1683
awk – Records and Fields (Records)
awk does not see input data as an endless string of characters, but it sees it having a format
or structure.
By default, each line is called a record and is terminated with a newline.
The Record Separator:
By default, the output and input record separator (line separator) is a carriage return
(newline), stored in built-in awk variables ORS and RS, respectively.
The ORS and RS values can be changed, but only in limited fashion
The $0 Variable:
An entire record is referenced as $0 by awk.
When $0 is changed by substitution or assignment, the value of NF, the number of fields,
may be changed.
The newline value is stored in awk’s built-in variable RS, a carriage return by default.
The NR Variable:
The number of each record is stored in awk’s built-in variable, NR.
After a record has been processes, the value of NR is incremented by one.
awk – Records and Fields (Records)
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500
EXAMPLE
UNIX Training $>awk '{print $0}' employees
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500
UNIX Training $>awk '{print NR, $0}' employees
1 Tom Jones 4424 5/12/66 543354
2 Mary Adams 5346 11/4/63 28765
3 Sally Chang 1654 7/22/54 650000
4 Billy Black 1683 9/23/44 336500
EXPLANATION
1. The awk variable $0 holds the current record. It is printed to the screen. By deafult, awk would also
print the record is the command were
awk '{print}' employees
2. Each record, $0, is printed as it is stored in the file and is preceded with the number of the record, NR
awk – Records and Fields (Fields)
Each record consists of words called fields that, by default, are separated by whitespace.
Each of these words is called a field, and awk keeps track of the number of fields in its built-
in variable, NF.
Each record starts at the at the first field, represented as $1, then moves to the second field,
$2, and so forth.
awk – Records and Fields (Fields)
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500
EXAMPLE
UNIX Training $>awk '{print NR, $1, $2, $5}' employees
1 Tom Jones 543354
2 Mary Adams 28765
3 Sally Chang 650000
4 Billy Black 336500
UNIX Training $>awk '{print $0, NF}' employees
Tom Jones 4424 5/12/66 543354 5
Mary Adams 5346 11/4/63 28765 5
Sally Chang 1654 7/22/54 650000 5
Billy Black 1683 9/23/44 336500 5
EXPLANATION
1. awk will print the number of the record (NR), and the first, second, and fifth fields (columns) of each
line in the file.
2. awk will print each record($0) in the file, followed by the number of fields.
awk – Records and Fields (Field Separators)
The Input Field Separator:
awk built-in, FS, holds the value of the input field separator. When the default value of
FS is used, awk separates fields by spaces and/or tabs, stripping leading blanks and
tabs.
The value of FS can be changed by assigning a new value to it, either in a BEGIN
statement or at the command line.
To change the value of FS at the command line, the –F option is used, followed by the
character representing the new separator.
awk – Records and Fields (Field Separators)
Tom Jones:4424:5/12/66:543354
Mary Adams:5346:11/4/63:28765
Sally Chang:1654:7/22/54:650000
Billy Black:1683:9/23/44:336500
EXAMPLE
UNIX Training $>awk -F: '{print $1, $2}' employees2
Tom Jones 4424
Mary Adams 5346
Sally Chang 1654
Billy Black 1683
UNIX Training $>awk -F: '/Tom Jones/{print $1, $2}' employees2
Tom Jones 4424
EXPLANATION
The –F option is used to reassign the value of the input field separator at the command line. When a
colon is placed directly after the –F option, awk will look for colon to separate the fields in the
employees2 file
awk – Records and Fields (Field Separators)
Tom Jones:4424:5/12/66:543354
Mary Adams:5346:11/4/63:28765
Sally Chang:1654:7/22/54:650000
Billy Black:1683:9/23/44:336500
EXAMPLE
UNIX Training $>awk 'BEGIN{FS=":"}{print $1, $2}' employees2
Tom Jones 4424
Mary Adams 5346
Sally Chang 1654
Billy Black 1683
EXPLANATION
BEGIN and FS can also be used to change to change the fields separator.
awk – Records and Fields (Field Separators)
The Output Field Separator:
The default output field separator is a single space and is stored in awk’s internal
variable, OFS.
The comma that is used to separate fields in print statements evaluates to whatever the
OFS has been set.
If the default is used, the comma inserted between $1 and $2 will evaluate to a single
space and the print function will print the fields with a space between them.
awk – Records and Fields (Field Separators)
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500
EXAMPLE
UNIX Training $>awk 'BEGIN{OFS=":"}{print $1, $5}' employees
Tom:543354
Mary:28765
Sally:650000
Billy:336500
EXPLANATION
The input field separator is a space and output field separator is changed to ‘:’. The first and fifth
fields are printed separated by :.
awk – Regular Expressions
A regular expression to awk is a pattern that consists of characters enclosed in forward
slashes.
awk supports the use of regular expression metacharacters (same as grep) to modify the
regular expression in some way.
If a string in the input line is macthes by the regaulr expression, the resulting condition is
true, and any actions associated with the expression are executed.
If no action is specified and an input line is matched by the regular expression, the record is
printed.
awk – Regular Expressions
Metacharacter What it does
^ Matches at the beginning of string
$ Matches at the end of string
. Matches a single character
* Matches for zero or more of the preceding characters
+ Matches for one or more of the preceding characters
? Matches for zero or one of the preceding characters
[ABC] Matches for any one character in the set of characters A, B, or C
[^ABC] Matches for any one character NOT in the set of characters A, B, or C
[A-Z] Matches for any one character in the range from A to Z
A|B Matches either A or B
(AB)+ Matches one or more sets of AB: e.g., AB, ABAB, ABABAB
\* Matches for a literal asterisk
& Used in replacement string to represent what was found in the search
string
awk – Regular Expressions
Metacharcters NOT supported
Metacharacter Function
\< >/ Word anchors
\( \) Backreferencing
\{ \} Repetition
awk – Regular Expressions
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500
EXAMPLE
UNIX Training $>awk '/Mary/' employees
Mary Adams 5346 11/4/63 28765
UNIX Training $>awk '/Mary/{print $1, $2}' employees
Mary Adams
UNIX Training $>awk '/^Tom/{print $1, $2, $3}' employees
Tom Jones 4424
UNIX Training $>awk '/^[A-Z][a-z]+ /' employees
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500
EXPLANATION
1. All lines in the employees file containing the regular expression pattern Mary are displayed
2. The first and second fields of all lines in the employees file containing the regular expression pattern Mary
are displayed
3. The first, second and third fields of all lines in the employees file containing the regular expression Tom as
starting of the line are displayed
4. All lines in the employees file beginning with an uppercase letter, followed by one or more lowercase letters,
followed by a space, are displayed.
awk – Regular Expressions (The match Operator)
The match operator, the tilde (~),is used to match an expression within a record or field.
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500
EXAMPLE
UNIX Training $>awk '$1 ~ /[Bb]ill/' employees
Billy Black 1683 9/23/44 336500
UNIX Training $>awk '$1 !~ /ly$/' employees
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
EXPLANATION
1. Any lines matching Bill or bill in the first field are displayed
2. Any lines not matching ly, when ly is at the end of the field are displayed
awk – Comparison Expressions
Comparison expressions match lines where the action is performed only if a certain
condition is true.
These expressions use relational operators and are used to compare numbers or strings.
Relational Operators
Arithmetic Operators
Operator Meaning Example
+ Add x+y
- Subtract x–y
* Multiply x*y
/ Divide x/y
% Modules x%y
^ Exponentiation x^y
awk – Comparison Expressions
Logical Operators
Operator Meaning Example
&& Logical AND a && b
|| Logical OR a || b
! NOT !a
awk – Examples for Comparison Expressions
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500
EXAMPLE
UNIX Training $>awk '$3==5346 {print $0}' employees
Mary Adams 5346 11/4/63 28765
UNIX Training $>awk '$3 > 5000 {print $1}' employees
Mary
UNIX Training $>awk '$2 ~ /Adam/ {print $0}' employees
Mary Adams 5346 11/4/63 28765
UNIX Training $>awk '$2 !~ /Adam/ {print $0}' employees
Tom Jones 4424 5/12/66 543354
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500
EXPLANATION
1. If the third field is exactly equal to 5346, print the line
2. If the third field is greater than 5000, awk prints the first field
3. If the second field matches or contains the regular expression Adam, the record is printed
4. If the second field does not match or does not contain the regular expression Adam, the record is
printed.
awk – Examples for Comparison Expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>awk '$7==5' datafile
western WE Sharon Gray 5.3 .97 5 23
eastern EA TB Savage 4.4 .84 5 20
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
UNIX Training $>awk '$2=="CT"{print $1, $2}' datafile
central CT
EXPLANATION
1. If the seventh field ($7) is equal to the number 5, the record is printed
2. If the second field is equal to the string CT, fields one and two ($1, $2) are printed. Strings must be
quoted.
awk – Examples for Comparison Expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>awk '$7 !=5' datafile
northwest NW Charles Main 3.0 .98 3 34
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
UNIX Training $>awk '$7 < 5 {print $4, $7}' datafile
Main 3
Dalsass 2
Chin 4
Hemenway 4
EXPLANATION
1. If the seventh field ($7) is not equal to the number 5, the record is printed
2. If the seventh field ($7) is less than 5, fields 4 and 7 are printed
awk – Examples for Comparison Expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>awk '$6 > .9 {print $1, $6}' datafile
northwest .98
western .97
southern .95
central .94
UNIX Training $>awk '$8 <= 17 {print $8}' datafile
15
17
9
13
EXPLANATION
1. If the sixth field ($6) is greater than .9, fields 1 and 6 are printed
2. If the eighth field ($8) is less than or equal to 17, is it printed
awk – Examples for Comparison Expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>awk '$8>10 && $8 < 17' datafile
southern SO Suan Chin 5.1 .95 4 15
central CT Ann Stephens 5.7 .94 5 13
UNIX Training $>awk '$2 == "NW" || $1 ~ /south/{print $1, $2}' datafile
northwest NW
southwest SW
southern SO
southeast SE
EXPLANATION
1. If the eighth field ($8) is greater than 10 AND less than 17, the record is printed. The record will be
printed only if both expressions are true
2. If the second field ($2) is equal to the string NW OR the first field ($1) contains the pattern south, the
first and second fields ($1, $2) are printed. The record will be printed is only one of the expressions is
true.
awk – Examples for Comparison Expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>awk '/^western/,/^eastern/' datafile
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
EXPLANATION
All records within the range beginning with the regular expression western are printed until a record
beginning with expression eastern is found. Records will start being printed again if the pattern
western is found and will continue to print until eastern or end of the file is reached.
awk – Examples for Comparison Expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>awk '{print ($7>4 ? "high " $7: "low " $7)}' datafile
low 3
high 5
low 2
low 4
low 4
high 5
high 5
high 5
EXPLANATION
If the seventh field ($7) is greater than 4, the print function gets the value of the expression after the
question mark (the string high and the seventh field); else the print function gets the value of
expression after the colon (the string low and the value of the seventh field).
awk – Examples for Comparison Expressions
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>awk '$3=="Lewis"{$3="Lewister"; print}' datafile
southwest SW Lewister Dalsass 2.7 .8 2 18
EXPLANATION
This is an assignment operator example.
If the third field ($3) is equal to the string Lewis, the action is to assign Lewister to the third field ($3)
and print the record.
awk –Variables (Numeric and String Constants)
Just mentioning a variable in your awk program causes it to exist.
A variable can be a string, a number, or both.
When it is set, it becomes the type of expression on the right-hand side of the equal sign.
Uninitialized variables have the value zero or “”, depending on the conect in which they are
used.
name=“Nancy” name is a string
x++ x is a number; x is initialized to zero and incremented by 1
number = 35 number if a number
name + 0 string is a number
number “” number is a string
awk –Variables (User-Defined Variables)
User-defined variables consist of letters, digits, and underscores, and cannot begin with a
digit.
Variables in awk are not declared.
awk infers data type by the context of the variable in the expression
To add one to an operand, the increment operator is used. The expression x++ is equivalent
to x=x+1
Similarly, the decrement operator subtracts one from its operand. The expression x– is
equivalent to x=x-1
A variable can be assigned a value at the command line and passed into an awk script.
awk –Variables (Built-In variables)
Variable Name Contents
ARGC Number of command-line argument
ARGV Array of command-line arguments
ENVIRON An array containing the values of the current environment variables
passed in from the shell
FILENAME Name of the current input file
FNR Record number in current file
FS The input field separator
IGNORECASE Turns off case sensitivity in regular expressions and string operations
NF Number of fields in the current record
NR Number of records so far
OFMT Output format for numbers
OFS Output field separator
ORS Output record separator
RS Input record separator
awk – BEGIN & END Patterns
The BEGIN pattern is followed by an action block that is executed before awk processes any
lines from the input file.
The BEGIN action is often used to change the value of the built-in variables, OFS, RS, FS,
and so forth, to assign initial values to user-defined variables and to print headers or titles as
part of the output.
END patterns do not match any input lines, but execute any actions that are associated with
the END pattern.
END patterns are handled after all lines of input have been processed.
awk – Examples for Variables
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>awk '/^north/{count+=1;print count}' datafile
1
2
UNIX Training $>awk '/^north/{count++;print count}' datafile
1
2
EXPLANATION
1. If the record begins with the regular expression north, a user-defined variable, count, is created;
count is incremented by 1 and its value is printed.
2. The auto-increment operator increments the user-defined variable count by 1. the value of count is
printed.
awk – Examples for Variables
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>awk '/^north/{print "the record number is " NR}' datafile
the record number is 1
the record number is 7
UNIX Training $>awk '{print NR, $0}' datafile
1 northwest NW Charles Main 3.0 .98 3 34
2 western WE Sharon Gray 5.3 .97 5 23
3 southwest SW Lewis Dalsass 2.7 .8 2 18
4 southern SO Suan Chin 5.1 .95 4 15
5 southeast SE Patricia Hemenway 4.0 .7 4 17
6 eastern EA TB Savage 4.4 .84 5 20
7 north NO Margot Weber 4.5 .89 5 9
8 central CT Ann Stephens 5.7 .94 5 13
EXPLANATION
1. If the record begins with the regular expression north, the string The records number is and the value of NR (record
number) are printed
2. The value of NR, the number of the current record, and the value of $0, the entire record, are printed
awk – Examples for Variables
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>awk 'NR==2,NR==5{print NR, $0}' datafile
2 western WE Sharon Gray 5.3 .97 5 23
3 southwest SW Lewis Dalsass 2.7 .8 2 18
4 southern SO Suan Chin 5.1 .95 4 15
5 southeast SE Patricia Hemenway 4.0 .7 4 17
UNIX Training $>awk '/^north/{print NR, $1, $2, $NF, RS}' datafile
1 northwest NW 34
7 north NO 9
EXPLANATION
1. If the value of NR is in the range between 2 and 5 (record numbers 2-5), the number of record (NR) and the record
($0) are printed.
2. If the record begins with the regular expression north, the number of the record (NR), followed by the first field, the
second field, the value of the last field (NF preceded by a dollar sign), and the value of RS (a newline) are printed.
Because the print function generates a newline by default, RS will generate another newline, resulting in double
spacing between records.
awk – Examples for Variables
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>awk 'NR==5 {print NF}' datafile
8
UNIX Training $>awk 'BEGIN{print "--------------EMPLOYEES----------"}'
--------------EMPLOYEES----------
EXPLANATION
1. If the number of the record (NR) is 5, the number of fields (NF) is printed
2. The BEGIN pattern is followed by an action block. The action is to print out the string --------------
EMPLOYEES---------- before opening the input file. Note that an input file has not been provided and
awk does not complain because any action preceded by BEGIN occurs first, even before awk looks
for an input file.
awk – Examples for Variables
northwest NW Charles Main 3 0.98 3 34
western WE Sharon Gray 5.3 0.97 5 23
southwest SW Lewis Dalsass 2.7 0.8 2 18
southern SO Suan Chin 5.1 0.95 4 15
southeast SE Patricia Hemenway 4 0.7 4 17
eastern EA TB Savage 4.4 0.84 5 20
north NO Margot Weber 4.5 0.89 5 9
central CT Ann Stephens 5.7 0.94 5 13
EXAMPLE
UNIX Training $>awk 'BEGIN{print "\t\t------------EMPLOYEES-----------\n"}{print $0}' datafile
------------EMPLOYEES-----------
EXAMPLE
UNIX Training $>awk -f awkfile2 datafile
NAME DISTRICT QUANTITY
------------------------------------------------------
Charles northwest 3
Sharon western 5
Lewis southwest 2
Suan southern 4
Patricia southeast 4
TB eastern 5
Margot north 5
Ann central 5
------------------------------------------
the total quantity is 33
the number of northern salespersons is 2.
Thank You