AWK Tutorial
What is AWK ?
AWK is a small interpreted language primarilly intended for parsing text streams, i.e. so-called "flat file" databases or piped text from other commands. While mainly used for "one-liners" similar to the way sed(1) is used, AWK is actually a turning-complete language and can be used for much more.
# ex. display non-system accounts w/ GECOS [1] info in /etc/passwd:
$ awk -F':' '$5 != "" && $NF !~ /nologin/{print $1}' /etc/passwd
The above command string will do the following:
- set the field separator to ":"
- read /etc/passwd line by line
-
print field $1 (UID) of any line matching the following criteria:
- field 5 (GECOS) is non-zero AND
- field NF (last field; acct shell) does NOT contain "nologin" [2]
[1] GECOS info is typically displayed by the finger(1) command, first & last name, office location and/or phone extension.
[2] system accounts usually do not allow interactive logins by setting the shell to "/usr/sbin/nologin", making it a useful filter.
Why learn AWK ?
Arguably AWK is mainly useful for users of commandline interfaces. Usually that means users of Unix-like systems, i.e. Linux, NetBSD. If that is you than learning some AWK is likely worth your time. Even though much of AWK's capabilities can be found in Perl or Python, AWK is much easier to learn and often runs faster.
Refs:
There are tons of AWK-related resources out there; here is a small sample.
Books:
- The AWK Programming Languages - Aho, Weinberger, Kernighan
- Sed & Awk, 2nd edition - Dougherty & Robbins
- Effective Awk Programming - Robbins
note: the 1st and 3rd are freely available in PDF form
Internet:
last compiled: 2024-10-15 11:00:58.792855