140 likes | 324 Views
AWK. Perl without the Fluff. What is AWK?. “AWK is a convenient and expressive programming language that can be applied to a wide variety of computing and data-manipulation tasks.” - AWK’s authors AWK is ideal when large amounts of data need to be manipulated. History of AWK.
E N D
AWK Perl without the Fluff
What is AWK? • “AWK is a convenient and expressive programming language that can be applied to a wide variety of computing and data-manipulation tasks.” - AWK’s authors • AWK is ideal when large amounts of data need to be manipulated.
History of AWK • Why is it called AWK? • Authors: • A -> Alfred Aho • W-> Peter Weinberger • K -> Brian Kernighan • Aho, Weinberger and Kernighan created AWK in 1977 at AT&T Bell Laboratories • Initial Release in 1978 for Version 7 Unix
History of AWK • Over the next 10 years, numerous implemenations were created • Nawk (interpreter), gawk (interpreter), tawk (compiler for Windows), mksawk (compiler), awkc++ (translates awk to C++), awk2c (translates to C) • In 1992 POSIX standard 1003.2 was introduced to “standardize applications and user interfaces to open systems.” • POSIX 1003.2 standardized more than just AWK • It standardized a majority the Unix/Linux shell utilities used today. cp, mv, kill, chown, chmod to name a few. • Also standardized Unix/Linux shell grammer, regular expressions, etc.
AWK vs. Perl • Perl can do everything AWK can and more • AWK is a simpler language that Perl • AWK is easier to learn than Perl • AWK is more widely available in the Unix/Linux environment • AWK code is quick to write • Clean and clear Perl code is better than unclear AWK code. But unclear Perl code is very difficult to understand
AWK: • AWK is a special purpose language • Block structured and Functional • Can be compiled or interpreted, depending upon the implementation • Variables essentially have two values, a numeric value (floating point), and a string value. • Default values of 0 and “”
AWK: • AWK decides whether to use the variable as a string or number depending on the operation currently being processed • All Arrays in AWK are associative • Loops and Conditionals • If, if-else, for, while • Special Conditionals • for (everyItem in array) • Similar to Perl’s foreach. Access to every item in an array • If (key in array) • Checks to see if the key exists in the array
AWK: • Awk provides some builtin functions for both strings, and numbers • Supports user defined functions • Provides easy access to the shell environment through the ENVIRON array • Very similar to Perl’s %ENV
AWK: • Input is read in as “records” • A record is split into “fields” • Each field is accessable as a variable, $0 - $n • $0 is the entire record • $1 is the first field in the record, $2 is the second … $n is the nth field in the record • Statements end with either a semicolon or newline • The basic syntax for the body of an AWK program is: pattern { action } • Both the pattern and action are optional, but one must be present
What practicle application is there for AWK? • Suppose you had a file containing a list of addresses: Jane Doe 456 3rd. Ave Boise, Id 83706 John Doe 123 2nd Ave Boise, Id 83715
What practicle application is there for AWK? • Say you wanted the Addresses printed out backwards. A program like this one would do that BEGIN { RS = "" ; FS = "\n" } { print "City, State and ZIP are:", $3 print "Address is:",$2 print "Name is:", $1 print "" }
Conclusion: • AWK is a quick way to manipulate data from the command line or from a script • AWK is not the fanciest way to accomplish data manipulation, but it works • AWK doesn’t have all the functionality of Perl • AWK is more simplistic and easier to learn than Perl • AWK is a great tool for any shell environment (even Windows)