next up previous contents index
Next: Regular Expressions Up: UNIX Shells Previous: Shell Scripts

Filters

A filter  is a command or program which gets its input from standard input, sends its output to standard output and may be used anywhere in a pipeline. The combination of UNIX filters ( grep, sort, awk, cut, paste) and the use of pipes is very powerful.

The grep  filter searches files for a pattern and prints only those lines matching that pattern.

sort  can sort or merge files.

 
               		  sort  [options] [field-specifier] [filename]

Sort is straightforward to use - read man sort to see what the options are and how to specify the sort fields. If a field is not specified, the sort key is the entire line. The sorted output goes to standard output.

awk  is a powerful pattern scanning and processing language. Although you will need to spend a little time learning how to use awk, it is very well suited to data-manipulation tasks. It handles internally what you would have to handle laboriously in a language like C or FORTRAN. For example, you can declare a field separator (spaces, colons, commas, tabs) and it will properly align and interpret the contents of a field according to the way you use it. Thus you can do in a few lines what would take many lines of FORTRAN.

A widely-available book on awk is The awk Programming Language [16]. There are several versions of awk, an old version and a new version which is not completely backwards compatible. The book mentioned above describes the new version, and you should use the new version unless you get a program that is explicitly for the old version. However, vendors are not consistent in what they call the two versions of awk: on Silicon Graphics and Sun, you must use nawk to get the new awk and awk is the old awk, but on RS/6000, awk is the new awk.



next up previous contents index
Next: Regular Expressions Up: UNIX Shells Previous: Shell Scripts



Alan Silverman
Wed Apr 12 16:54:02 METDST 1995