Grep, Awk and Sed

From Exterior Memory
Jump to: navigation, search

Grep, awk and sed are small UNIX tools for simple matching and reformatting of text lines. Grep matches regular expressions; awk is a simple programming languages (it precedes perl); sed is a stream editor.

Examples

Awk

Table Formatting

The output of port list active is formatted as follows:

adns                           @1.4            net/adns
apache24-devel                 @2.4.7          www/apache24-devel
apr                            @1.4.8          devel/apr
apr-util                       @1.5.2          devel/apr-util
...

To sort based on category, reformat with awk and sort:

% port list active | awk '{print $3,$1}' | sort | awk '{printf " %-30s %s\n",$2,$1}'

aquaterm                       aqua/aquaterm
bzip2                          archivers/bzip2
gzip                           archivers/gzip
lzo2                           archivers/lzo2
p7zip                          archivers/p7zip
...
This article is unfinished.