University of Southern California

ITS Information Technology Services

A division of the Office of the Chief Information Officer

Filter Commands

Introduction

Several utilities are available to manipulate and filter files. Three of the more common utilites are discussed here: grep, sed and awk.

For further explanation of grep, see the ITS documentation at The Grep Command.

Sed

The sed utility copies the named filename to the standard output, edited according to a script of commands.

Here is an example of a <script> command:

In this example, sed will substitute all the occurances of "dog" with "wolf" in the file called animals.

If the expression contains a space, surround it with quotation marks as shown in the example below:

Here is another example of a <script> command:

In this example, sed will transform all the occurances of "unix" into "UNIX". The variables <string1> and <string2> must have the same number of characters.

For more examples of <script> commands and for more information on sed, type man sed.

Awk

The awk utility scans each input file for lines that match any of a set of patterns specified. For each pattern specified there may be an associated action performed.

The following example will print to the screen every line which contains the pattern "dog" in the file named animals.

This next example will print "Found It" to the screen for every line which starts with the pattern "Tiger" in the file named animals.

awk is a very powerful and complex utility. For more information on awk, type man awk.

Last updated:
February 03, 2011

UNIX Documentation

The use of all USC computing resources is governed by the USC Computing Policies.